jQuery Ajax调用未调用成功
我面临一个ajax请求问题。每当我运行以下命令时,我都会得到一个jQuery Ajax调用未调用成功,jquery,ajax,json,post,Jquery,Ajax,Json,Post,我面临一个ajax请求问题。每当我运行以下命令时,我都会得到一个x(正如下面请求中的错误函数所生成的那样),但我应该会得到勾号图像,因为我确认调用确实在系统中进行了更改 谁能帮我找出我做错了什么 Ajax调用: $.ajax({ url: "/URL", type: "POST", dataType: 'json', data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
x
(正如下面请求中的错误函数所生成的那样),但我应该会得到勾号图像,因为我确认调用确实在系统中进行了更改
谁能帮我找出我做错了什么
Ajax调用:
$.ajax({
url: "/URL",
type: "POST",
dataType: 'json',
data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
success: function(){
$("#user_1_image").attr("src","http://domain.com/check.png");
},
error: function(){
$("#user1_status").html("x");
}
});
将作为输出的数据:
{status: 'OK', payload: ''}
如果您从指定的url获取有效的json,那么这将起作用,因为在代码中,您没有在success函数中传递数据:
$.ajax({
url: "/URL",
type: "POST",
dataType:'json',
data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
success: function(data){
if(data){
$("#user_1_image").attr("src","http://domain.com/check.png");
}
},
error: function(){
$("#user1_status").html("x");
}
});
如果json字符串中存在解析错误,请先检查json字符串
Parse error on line 1:
{ action: 'assoc_users
-----^
Expecting 'STRING', '}'
我已经更正了你的json字符串并试用了它。试试这个,它行得通
$(document).ready(function() {
var actions = { "action": "assoc_users", "username": "testuser", "name": "testname" };
$.ajax({
url: "/URL",
type: "POST",
data: actions,
dataType: "json",
success: function(data){
alert('action'+data.action+'username'+data.username+'name'+data.name); // this is to check whats coming from the server side
$("#user_1_image").attr("src","http://domain.com/check.png");
},
error: function(jqXHR, exception){
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
在php文件中
<?php echo json_encode($_POST); ?>
请使用它,可能是引号造成了问题,所以请尝试一下
$.ajax({
url: '/URL',
type: 'POST',
dataType: 'json',
data: { action: 'assoc_users', username: 'testuser', name: 'testname' },
success: function(){
$('#user_1_image').attr('src','http://domain.com/check.png');
},
error: function(){
$('#user1_status').html('x');
}
});
控制台有错误吗?我没有使用控制台或任何东西。基本上是一个返回值的PHP页面我想@ExplosionPills的意思是你在Firefox中是否有javascript错误?没有,ExplosionPills会询问你是否正在使用浏览器的控制台(例如Firefox中的Firebug)跟踪javascript是否报告了问题。成功:function(){来自url的内容是什么?你没有使用从url返回的任何内容我收到了相同的问题删除数据类型尝试第二个如果你从该url获取json,那么你必须通过success函数传递它,如果有数据,那么将attr提供给img。