1

I am very new to asp.net web api. I am making a simple call with jquery, cross-domain. Everything seems to work properly and when I even check the results I get back, I see that it sends json back to me, but the call fails with the “Uncaught SyntaxError: Unexpected token :” error. I am not sure why its failing, Thanks for any help.

$.ajax({
    url: 'http://webapidomain.domain.com/api/Register?firstName=' + firstName + '&lastName=' + lastName + '&' + 'email=' + email + '&password=' + password,
    type: 'GET',
    dataType: 'jsonp',
    contentType: 'application/json',
    success: function (result) {
        alert(result);
    },
    complete: function () {
        $.mobile.hidePageLoadingMsg();
    }
});
1

2 Answers 2

2

Web API does not support JSONP out of the box. You would need a JSONP media-type formatter. (The media-type formatter is the object that serializes the data to a particular format, such as JSON or XML.)

Rick Strahl has some code here that might help: http://www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API (I haven't tried it.)

The reason you are getting back JSON when you asked for JSONP is that when Web API cannot match the type that you requested, it returns the first format in its list, which by default is JSON.

Sign up to request clarification or add additional context in comments.

Comments

0

if you are making a cross domain call it is by default blocked by many browsers by the SAME-ORGIN Policy. If you need to work around that you need to use JSONP (if you can modify the output produced by the end point that you are calling i.e you own the domain) else you need to use your web server as a proxy and connect to that end point and query to your web endpoint to get the corresponding data.

Also there are certain websites that will do this (act as the proxy to generate JSONP result) for you and produce a jsonp result.

3 Comments

as you can see I am using jsonp. Everything works ok its just when recieving the data back its not in json and its receiving that error.
its not about you using jsonp even the server should send results back in a format supported by jsonp
something like this: jsonp123({"name" : "xyz", "id" : "10", "blog" : "xyz.com"}); where jsonp123 is a function jquery defines

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.