使用 JSON 返回对 PHP 脚本的 jQuery AJAX 调用 [英] jQuery AJAX Call to PHP Script with JSON Return

查看:27
本文介绍了使用 JSON 返回对 PHP 脚本的 jQuery AJAX 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'datatype='json'.我也尝试将其更改为 data.statusdata['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.

任何帮助将不胜感激.

推荐答案

使其成为 dataType 而不是 datatype.

Make it dataType instead of datatype.

并在 php 中添加以下代码,因为您的 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 和 JSONP 的正确内容类型

萤火虫中可见的响应是文本数据.检查响应头的 Content-Type 以验证响应是否为 json.dataType:'json' 应该是 application/jsondataType:'html' 应该是 text/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'.

这篇关于使用 JSON 返回对 PHP 脚本的 jQuery AJAX 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆