Javascript jQuery AJAX调用PHP脚本并返回JSON

Javascript jQuery AJAX调用PHP脚本并返回JSON,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我已经用这一个把我的头撞到了砖墙上,我在stackoverflow上尝试了很多解决方案,但找不到一个有效的 基本上,当我发布AJAX时,PHP返回JSON,但AJAX显示未定义的值,而不是值: JS: /* attach a submit handler to the form */ $("#group").submit(function(event) { /* stop form from submitting normally */ event.preventDefault

我已经用这一个把我的头撞到了砖墙上,我在stackoverflow上尝试了很多解决方案,但找不到一个有效的

基本上,当我发布AJAX时,PHP返回JSON,但AJAX显示未定义的值,而不是值:

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);
      }   
    }); 
});
  $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'));
        }
    }
{"status":"success","message":"success message"}
PHP

  /* 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);
      }   
    }); 
});
  $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'));
        }
    }
{"status":"success","message":"success message"}
来自firebug的JSON结果:

  /* 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);
      }   
    }); 
});
  $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'));
        }
    }
{"status":"success","message":"success message"}
AJAX将JSON结果显示为未定义,我不知道为什么。我已经尝试显示添加
dataType='json'
dataType='json'
。我还尝试将其更改为
data.status
data['status']
:但仍然没有乐趣


任何帮助都将不胜感激。

请尝试从服务器发送内容类型标题,并在回显之前使用此标题

header('Content-Type: application/json');

将其设置为
dataType
,而不是
dataType

并在php中添加以下代码,因为ajax请求需要json,并且只接受json

header('Content-Type: application/json');

firebug中可见的响应是文本数据。检查响应头的
内容类型
,验证响应是否为json。对于
数据类型:'json'
,它应该是
application/json
;对于
数据类型:'html'
,它应该是
text/html
,我建议您使用:

var returnedData = JSON.parse(data);

将JSON字符串(如果只是文本)转换为JavaScript对象。

使用parseJSON jquery方法将字符串转换为对象

var objData = jQuery.parseJSON(data);
现在您可以编写代码了

$('#result').html(objData .status +':' + objData .message);

您的数据类型错误,请将数据类型更改为数据类型。

设置“应用程序/json”标题
标题('Content-Type:application/json')
datatype
应该有大写字母
T
datatype:'json'
。从理论上讲,它应该从回答中推断出类型,因此这可能无法解决问题。@hank或@Rory的建议应该足够了。如果设置了正确的mimetype,则无需指定
dataType
Hi,谢谢!大多数帖子都说这个标题('Content-Type:application/json');这不是必需的!显然是这样!如果这对你有帮助,我很高兴。我知道这样的事情不起作用时的感觉。是的,我过去也曾为此撞过墙:D但使用了不同的服务器端语言。我确实有一个脚本仍然不起作用,我正在从表中删除一行,然后返回json,但是json前面似乎有删除行的ID:
38{“status”:“success”,“message”:“组已被删除”}
您必须打印并查看php发送的内容,并进行相应的修复。这是我的问题的解决方案。如果您使用成功回调,您已经将生成的字符串解析为javascript对象,并将其作为参数放入成功回调函数中。