74H1R
74H1R

Reputation: 3514

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.

enter image description here

Upvotes: 0

Views: 3653

Answers (3)

Peter Knego
Peter Knego

Reputation: 80340

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

Upvotes: 0

robertc
robertc

Reputation: 75777

Have you configured CORS on your AppEngine app?

Upvotes: 1

Igor Artamonov
Igor Artamonov

Reputation: 35951

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

Upvotes: 0

Related Questions