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して return コールバック$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 に答える