2

I have created REST API in MVC4, it is working fine when I compose request from fiddler. But in my application, I need to call through jsonp because it would cross domain request. But when I'm calling this service it gives me error as shown below:

Jquery JsonP Call ..

$.ajax({
        type: "POST" ,
        url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf&callback=?",
        data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
        cache: false,
        contentType: "application/json; charset=utf-8",
        success: function (response) {
            if (callback)
                callback(response.d);
        },
        error: function (response) {
            if (callback)
                error(response.d);
        },
    });

Error: enter image description here

1

1 Answer 1

2

Right now you are not doing JSONP. It's still POST request. To make it JSONP you need simply to add dataType: "jsonp" to you $.ajax() call. You can also remove some other redundancy parameters like content-type and 'callback' param (but that's optional). So, your code should looke like:

$.ajax({
    url: "http://127.0.0.1:81/api/sites/GetDomainAvailability?apikey=asfasfdsf",
    data: { SubDomain: subDomain, ParentDomain: parentDomain, ResellerId: resellerId },
    datatype: "jsonp",
    cache: false,
    success: function (response) { /* ... */ },
    error: function (response) { /* ... */ },
});

Be also ready, that your request will be transformed to a GET one and will look like /GetDomainAvailability?apikey=key&callback=jquery123&SubDomain=sss&ParentDomain=ppp&ResellerId=123&_=4398572349857

So, prepare your server-side code for that.

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

Comments

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.