![](/img/trans.png)
[英]Get JSON response using jQuery.ajax request to an appengine URL?
[英]How to get JSON response in jQuery.ajax request to an appengine URL?
我在Google App Engine托管的URL服務返回JSON內容類型。
示例URL是this ,在瀏覽器中可以完美運行。 還通過http://jsonlint.com進行了驗證。
我正在使用以下jQuery.ajax調用來獲取響應。
jQuery.ajax({
type: "get",
url: "http://trim-pk.appspot.com/do?url=http://www.google.com",
dataType: "json",
success: function(response) {
alert(response);
}
});
怎么了 為什么我沒有得到答復。 是空的
我嘗試使用contentType: "application/json; charset=utf-8"
和contentType: "application/json; charset=ISO-8859-1"
,但是得到了500。
以下是我的Firebug輸出。
您是否在AppEngine應用上配置了CORS ?
:
和/
是Url中的保留字符,具有特殊含義。
要在網址參數中使用它們,必須對它們進行“網址編碼” 。 瀏覽器會自動執行此操作。
在Javascript中,您必須通過encodeURIComponent()
函數手動執行此操作。 這應該可以解決問題:
jQuery.ajax({
type: "get",
url: "http://trim-pk.appspot.com/do?url=" + encodeURIComponent("http://www.google.com"),
dataType: "json",
success: function(response) {
alert(response);
}
});
更新:用encodeURI
替換了encodeURIComponent
。
嘗試
jQuery.ajax({
type: "get",
url: "http://trim-pk.appspot.com/do",
data: {
url: "http://www.google.com"
},
dataType: "json",
success: function(response) {
alert(response);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.