2012-11-17 77 views
1

我試圖從Geobytes獲取數據。其中一個模板返回JSON,我需要跨域訪問它。使用AJAX使用AJAX跨域數據

我寫這2個功能

function getCountry(ip) { 
    var surl = "http://www.geobytes.com/IpLocator.htm?GetLocation&template=json.txt"; 
    $.ajax({ 
     url: surl, 
     data: '{"ipaddress":"' + ip + '"}', 
     dataType: "jsonp", 
     processData: false, 
     jsonpCallback: "jsonpcallback", 
     error: function (xhr, status, error) { 
      alert(xhr.responseText); 
     } 
    }); 
} 

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

成功執行的調用,這些都是我的響應頭:

HTTP/1.1 200 OK 
Date: Sat, 17 Nov 2012 12:43:54 GMT 
Expires: 0 
Content-type: text/html 
Transfer-Encoding: chunked 

返回的數據是JSON,但我得到

warning: Resource interpreted as Script but transferred with MIME type text/html: "http://www.geobytes.com/IpLocator.htm?GetLocation&template=json.txt&callback=jsonpcallback&{%22ipaddress%22:%22200.167.254.166%22}&_=1353148931121"

Error on the remote IpLocator.htm: Uncaught SyntaxError: Unexpected token :

錯誤發生在返回的數據上

{"geobytes":{"countryid":117, 

我想也許是因爲它是117而不是「117」,但我顯然無法控制返回的數據。試圖添加「processData = false」,但沒有幫助。

我已經添加了錯誤處理的AJAX並獲得「parsererror」在狀態

我怎樣才能解決這個問題?

+0

會發生什麼如果您跳過ajax調用,並試圖在固定字符串上調用$ .parseJSON?換句話說,複製結果,修復「117」問題並查看是否解決了分析錯誤。這會告訴你,如果這是問題。 – davidethell

回答

0

謝謝no.andrea指着我正確的方向!

由於我使用MVC3我添加了一個新的控制器

[HttpPost] 
public JsonResult JsonGetCountry(string ip) 
{ 
    try 
    { 
     var sb = new StringBuilder(); 
     sb.Append("http://www.geobytes.com/IpLocator.htm?GetLocation"); 
     sb.Append("&template=json.txt"); 
     sb.Append("&IpAddress="); 
     sb.Append(ip); 

     var webClient = new WebClient(); 
     var data = webClient.OpenRead(sb.ToString()); 

     if (data == null) { return Json(""); } 

     var reader = new StreamReader(data); 
     var msg = reader.ReadToEnd(); 
     data.Close(); 
     reader.Close(); 

     return Json(msg); 
    } 
    catch (Exception ex) 
    { 
     throw new Exception(ex.Message, ex); 
    } 
} 

並更新了jQuery函數來

function getCountry(ip) { 
    $.ajax({ 
     type: "POST", 
     url: "/My-Shop/IP/JsonGetCountry", 
     data: '{"ip":"' + ip + '"}', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (msg) { 
      var a = $.parseJSON(msg); 
      alert(a.geobytes.iso2); 
     }, 
     error: function (ex) { 
      alert(ex.statusText); 
    } 
}); 

這修好了我所有的問題:)

2

嘗試修改字段的dataType是這樣的:

 
... 
dataType: "jsonp json", 
... 

這樣喲得到的數據將被解析爲JSON。

以下文件:

The type of data that you're expecting back from the server. If none is specified, jQuery will try >to infer it based on the MIME type of the response.

在你的情況你得到的MIME是text/HTML,加入「JSON」值到該數據類型,你告訴jQuery的對待響應爲JSON而不是文字。

如果服務不支持JSONP,你可以使自己代理的頁面來處理請求,或使用YQL如下所述:Cross-Domain request when server does NOT support JSONP

+0

即使有這種變化,我仍然得到相同的2警告/錯誤 – kooshka

+0

@kooshka你確定這項服務支持jsonp嗎?我跑了代碼,似乎並不如此。 –

+0

不,它不支持它,但這不是一個表演塞。如果您在瀏覽器中輸入「http://www.geobytes.com/IpLocator.htm?GetLocation&template=json.txt&IpAddress=200.167.254.165」,則會返回json,並使用我的ajax函數獲取它。問題是解析器錯誤 – kooshka