2012-12-15 57 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