简体   繁体   English

如何在jQuery.ajax请求中获取对Appengine URL的JSON响应?

[英]How to get JSON response in jQuery.ajax request to an appengine URL?

My URL service hosted at Google App Engine return JSON content type. 我在Google App Engine托管的URL服务返回JSON内容类型。

The example URL is this , that works perfect in browser. 示例URL是this ,在浏览器中可以完美运行。 Also validated from http://jsonlint.com . 还通过http://jsonlint.com进行了验证。

I am using following jQuery.ajax call to get the response. 我正在使用以下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);
} 
});

What's went wrong? 怎么了 Why I am not getting the response. 为什么我没有得到答复。 It's null. 是空的

I have tried with contentType: "application/json; charset=utf-8" and contentType: "application/json; charset=ISO-8859-1" , but got 500. 我尝试使用contentType: "application/json; charset=utf-8"contentType: "application/json; charset=ISO-8859-1" ,但是得到了500。

Following is my Firebug output. 以下是我的Firebug输出。

在此处输入图片说明

您是否在AppEngine应用上配置了CORS

: and / are reserved characters in Url and have a special meaning. :/是Url中的保留字符,具有特殊含义。

To use them in Url parameters, you must "Url encode" them. 要在网址参数中使用它们,必须它们进行“网址编码” Browsers do that automatically. 浏览器会自动执行此操作。

In Javascript you must do this by hand via encodeURIComponent() function. 在Javascript中,您必须通过encodeURIComponent()函数手动执行此操作。 This should do the trick: 这应该可以解决问题:

jQuery.ajax({
    type: "get",
    url: "http://trim-pk.appspot.com/do?url=" + encodeURIComponent("http://www.google.com"),
    dataType: "json",
    success: function(response) {
        alert(response);
    } 
});

Update: replaced encodeURI with encodeURIComponent . 更新:encodeURI替换了encodeURIComponent

try 尝试

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM