简体   繁体   中英

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.

The example URL is this , that works perfect in browser. Also validated from http://jsonlint.com .

I am using following jQuery.ajax call to get the response.

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.

Following is my Firebug output.

在此处输入图片说明

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

: and / are reserved characters in Url and have a special meaning.

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. 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 .

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);
    } 
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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