jQuery的AJAX调用PHP脚本使用JSON返回 [英] jQuery AJAX Call to PHP Script with JSON Return
问题描述
我一直在砸我的头撞墙用这个,我已经尝试了解决方案的计算器上的负荷,但不能找到一个工程!
I've been smashing my head against a brick wall with this one, i've tried loads of the solutions on stackoverflow but can't find one that works!
基本上当我后我的AJAX的PHP返回JSON但AJAX显示未定义的而不是值:
Basically when I POST my AJAX the PHP returns JSON but the AJAX shows Undefined instead of the value:
JS
/* attach a submit handler to the form */
$("#group").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/*clear result div*/
$("#result").html('');
/* get some values from elements on the page: */
var val = $(this).serialize();
/* Send the data using post and put the results in a div */
$.ajax({
url: "inc/group.ajax.php",
type: "post",
data: val,
datatype: 'json',
success: function(data){
$('#result').html(data.status +':' + data.message);
$("#result").addClass('msg_notice');
$("#result").fadeIn(1500);
},
error:function(){
$("#result").html('There was an error updating the settings');
$("#result").addClass('msg_error');
$("#result").fadeIn(1500);
}
});
});
PHP
$db = new DbConnector();
$db->connect();
$sql='SELECT grp.group_id, group_name, group_enabled, COUNT('.USER_TBL.'.id) AS users, grp.created, grp.updated '
.'FROM '.GROUP_TBL.' grp '
.'LEFT JOIN members USING(group_id) '
.'WHERE grp.group_id ='.$group_id.' GROUP BY grp.group_id';
$result = $db->query($sql);
$row = mysql_fetch_array($result);
$users = $row['users'];
if(!$users == '0'){
$return["json"] = json_encode($return);
echo json_encode(array('status' => 'error','message'=> 'There are users in this group'));
}else{
$sql2= 'DELETE FROM '.GROUP_TBL.' WHERE group_id='.$group_id.'';
$result = $db->query($sql2);
if(!$result){
echo json_encode(array('status' => 'error','message'=> 'The group has not been removed'));
}else{
echo json_encode(array('status' => 'success','message'=> 'The group has been removed'));
}
}
从萤火 JSON结果
{"status":"success","message":"success message"}
AJAX显示JSON结果是不确定的,我没有一个线索,为什么。我曾尝试显示添加的dataType =json的
和数据类型='json的
。我自己也尝试将其更改为 data.status
和数据['状态']
:仍然没有喜悦,虽然。
AJAX Displays the JSON result as Undefined and I dont have a clue why. I have tried displaying adding dataType='json'
and datatype='json'
. I have also tried changing it to data.status
and data['status']
: still no joy though.
任何帮助将是非常美联社preciated。
Any help would be really appreciated.
推荐答案
请它的dataType
,而不是的数据类型
。
和PHP中添加以下code作为Ajax请求期待JSON和不接受任何东西,但JSON。
And add below code in php as your ajax request is expecting json and will not accept anything, but json.
header('Content-Type: application/json');
在萤火可见的反应是文本数据。检查内容类型
响应头进行验证,如果响应是JSON。它应该是应用程序/ JSON
为数据类型:JSON
和的text / html
为数据类型:HTML
The response visible in firebug is text data. Check Content-Type
of the response header to verify, if the response is json. It should be application/json
for dataType:'json'
and text/html
for dataType:'html'
.
这篇关于jQuery的AJAX调用PHP脚本使用JSON返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!