1

这是我第一次使用 JSON。我正在尝试使用 ajax 从 php 脚本中获取 JSON 数据。但我收到此错误“Error.Parsing JSON Request failed”“undefined”。帮帮我这是我的 php 脚本test.php

    $data='{
  "one": "One",
 "two": "Two",
 "three": "Three"
   }';

 header('Content-type: application/json');
 echo json_encode($data);

在这里我得到数据getdata.php

    var sURL = 'http://www.example.com/test.php';   

$.ajax({
            type: "GET",
                            url:sURL,
                            dataType:"jsonp",
                            crossDomain:true,
                            data: {transid:trans_id , bookingdate: dateVal, bookingtime: timeVal, People: peopleVal,affiliateid: affiliate },
                            async: false,
                            contentType:"application/json; charset=utf-8",
                            success: function (data){
                                                                                    var result = JSON.parse(data);
                                            alert(result);
                                            },
                                            error: function (x, e) {

        if (x.status == 0) {
            alert(x.response);
            alert(x + " " + e);
            alert('You are offline!!\n Please Check Your Network.');
        }
        else if (x.status == 404) {
            alert('Requested URL not found.');
        }
        else if (x.status == 500) {
            alert('Internel Server Error.');
        }
        else if (e == 'parsererror') {

            alert('Error.\nParsing JSON Request failed.' + e.statusText);
            alert(x.response);
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }

                            });

这是我的第一个问题,请原谅任何错误

4

2 回答 2

2

在您使用jsonp时,添加如下回调函数

var sURL = 'http://www.example.com/test.php';   

$.ajax({
            type: "GET",
                            url:sURL,
                            dataType:"jsonp",
                            jsonp : "callback",
                            jsonpCallback: "jsonpcallback",
                            crossDomain:true,
                            data: {transid:trans_id , bookingdate: dateVal, bookingtime: timeVal, People: peopleVal,affiliateid: affiliate },
                            async: false,
                            contentType:"application/json; charset=utf-8",
                            success: function (data){
                                                                                    var result = JSON.parse(data);
                                            alert(result);
                                            },
                                            error: function (x, e) {

        if (x.status == 0) {
            alert(x.response);
            alert(x + " " + e);
            alert('You are offline!!\n Please Check Your Network.');
        }
        else if (x.status == 404) {
            alert('Requested URL not found.');
        }
        else if (x.status == 500) {
            alert('Internel Server Error.');
        }
        else if (e == 'parsererror') {

            alert('Error.\nParsing JSON Request failed.' + e.statusText);
            alert(x.response);
        } else if (e == 'timeout') {
            alert('Request Time out.');
        } else {
            alert('Unknow Error.\n' + x.responseText);
        }
    }

 });

function jsonpcallback(rtndata) {
  alert(rtndata.one);
}

在 PHP 中 make$data as array然后使用json_encode()返回回调。

 $data=array(
  "one"=>"One",
  "two"=>"Two",
  "three"=>"Three"
 );
 header('Content-type: application/json');
 echo $_GET['callback']. '('. json_encode($data) . ')';  
于 2012-12-15T08:26:00.783 回答
0

改变你的

     dataType:"jsonp",

dataType:"json",
于 2012-12-15T08:22:27.780 回答