Suhas
Suhas

Reputation: 37

Ajax Call using jsonp for calling rest webservice from other domain

How I use the jsonp when call to rest webservice from different domain.becouse my simple json does not working for rest webservice.how can i call rest webservice for ajax call.i use both jsonp and javascript for that.server responce i seen on only browser in firebug but how to show it on my application

my JS code.

function callService() { $.ajax({ type: varType, //GET or POST or PUT or DELETE verb url: varUrl, // Location of the service data: varData, //Data sent to server crossDomain: true, cache: false, async: false, contentType: varContentType, // content type sent to server dataType: varDataType, //Expected data format from server processdata: varProcessData, //True or False success: function (msg) {//On Successfull service call alert("Server Responce Successfully!.."); }, error: function (msg) { alert('error ' + msg.d); } }); } function countryProvinceWCFJSONMulti() {

        var head = document.getElementsByTagName('head')[0];
        script = document.createElement('script');
        script.type = 'JSON';
        script.src = "http://192.168.15.213/myservice/Service.svc/GetEvents";
        script.type = "text/javascript";
        head.appendChild(script);
        varType = "GET";           
        varData = '{"username": "' + "Suhasusername" + '","password": "' + "suhaspassword" + '"}';
        varContentType = "application/JSONP; charset=utf-8";
        varDataType = "JSONP";
        varProcessData = true;
        callService();
    } 

Upvotes: 0

Views: 130

Answers (1)

digiliooo
digiliooo

Reputation: 841

If the same-origin policy isn't an issue, your code should look something like this:

$.ajax({ url: url,
         dataType: 'jsonp',
         contentType: 'application/json',
         success: function (data) {
               alert("Server Response Successful!..");
        }
    });

Here is a question that talks about circumventing the same-origin policy

Upvotes: 0

Related Questions