Andrew Kalashnikov
Andrew Kalashnikov

Reputation: 3173

JQuery ajax call to cross-domain webservice

I would like consume cross-domain web-service from client with jquery

function TestService() {
    $.ajax({
        url: "http://service.asmx/GetGeoCompletionList",
        data: { "prefixText":"eka", "count":"10", "contextKey":"Trace_0$Rus" },
        dataType: "jsonp",
        type: "GET",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert(data);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.statusText);
        }
    });
}

At the error hander I have: textStatus=parseerror

XMLHttpRequest has status 200 and readyState 4

errorThrown is jQuery16103495572647140...78197139 was not called

I've spent on it for a lot of hours and couldn't make it work. Can u help me?

UPDATED

Thanks, I change to GET, and fix my data string.

Service returns valid JSON object. I can see it at firebug on other site, which consume this service. But that site is getting common json(since it has one domain).

So, if the web-service returns valid JSON(not jsonp), I can't use the same method with jsonp? What can I do for consiming json web-service from other domain?

Upvotes: 2

Views: 11318

Answers (3)

Jerod Venema
Jerod Venema

Reputation: 44632

Summary of problems:

  1. Make sure you're using valid JSON as @Quentin mentioned.
  2. Make sure you're using GET requests, as @lonesomeday mentioned
  3. Make sure the response from the server is JSONP, as seen here

Upvotes: 2

lonesomeday
lonesomeday

Reputation: 237817

You cannot do cross-domain POST requests using JSONP. JSONP works by adding script tags to the page. script tags always fetch their source using GET HTTP requests. You won't get any data posted.

Upvotes: 3

Quentin
Quentin

Reputation: 943097

That string you are passing and claiming is JSON isn't JSON.

Only " characters may be used to quote strings.

Also, you can't make a cross domain JSON-P POST request.

Upvotes: 3

Related Questions