2012-12-15 85 views
1

這是我第一次使用JSON。我想從使用ajax.But PHP腳本JSON數據我收到此錯誤「Error.Parsing JSON請求失敗」,「不確定」。幫助我,這是我的PHP腳本test.php的從php獲取json數據並在jquery中使用它

$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); 
     } 
    } 

          }); 

這是我的第一個問題,所以藉口任何錯誤

回答

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做然後用json_encode(),返回回調。

$data=array(
    "one"=>"One", 
    "two"=>"Two", 
    "three"=>"Three" 
); 
header('Content-type: application/json'); 
echo $_GET['callback']. '('. json_encode($data) . ')'; 
+0

did not help.now火蟲也給了SyntaxError的錯誤:無效標籤 [Break On This Error] \t {「one」:「One」,「two」:「Two」,「three」:「Three」 }(第1行,第1列) –

+0

@KaranTuteja:嘗試myanswer結合這個答案,它應該工作,因爲json_encode方法只接受數組.. ..! –

+0

現在它在螢火蟲中給出這個錯誤SyntaxError:JSON.parse:意外字符 [Break On This Error] \t var result = JSON.parse(data); –

0

更改

 dataType:"jsonp", 

dataType:"json", 
+0

嘗試它沒有幫助。它不影響任何反正jsonp用於跨域。 –

+0

你是什麼意思crossdomain(在同一代理)?或來自外部來源? – Jai