Slatiner
Slatiner

Reputation: 56

Accessing a remote web service with jQuery

I'm developing a mobile application which requires access to a certain remote web service. I'm using jQuery.ajax(), and because of the the same origin policy, I'm forced to do my request using JSONP. My client request is so:

$.ajax({
    type: "GET",
    url: "http://www.foo.jws/bar",
    contentType: "application/json; charset=utf-8",
    data: {},
    dataType: 'jsonp', 

    success: function(msg) {
       console.log(msg);
    },
    error: function() {
      console.log('error');
    }

});

The error I keep receiving is "Uncaught SyntaxError: Unexpected identifier"

What am I doning wrong?

EDIT: The data in the WS is in XML

Upvotes: 2

Views: 303

Answers (2)

Gromer
Gromer

Reputation: 9931

Your error callback code is incorrect. I'm not sure if this was just because you typed the code in here, or copy/pasted it. Should be:

error: function (jqXHR, textStatus, errorThrown) {
    console.log('error');
}

You can then get some more information about what's going on with your call from the arguments that come into the error callback.

Edit: Okay, since you're getting XML back from the server, you need to change dataType from jsonp to xml. The dataType option tells jQuery what type of data to expect on the return.

$.ajax({
    type: "GET",
    url: "http://www.foo.jws/bar",
    contentType: "application/json; charset=utf-8",
    data: {},
    dataType: 'xml', 
    success: function(msg) {
       console.log(msg);
    },
    error: function() {
      console.log('error');
    }
});

Upvotes: 0

Ry-
Ry-

Reputation: 224963

You're missing a function here.

error: function() {
    console.log('error');
}

Also, take out the trailing comma.

Upvotes: 2

Related Questions