[英]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.