Jquery Cross-Domain ajax call using JSONP
Hi everyone. We all know the role of ajax and its implementation. We can use ajax in asp.net as well as in javascript/jquery. But there is a limitation of same origin policy. i.e. we can only use ajax to post and get requests within our site. We can call webservices but that is from the code behind only. Even if we use any scripting language it restricts us calling a third party domain. Also over SSL javascript/jquery's ajax call gives up easily. But using jsonp with jquery's ajax api call we can target the ajax call outside the scope of our website.
$(document).ready(function() {
var surl = "http://www.anotherdomain.com/webservice.asmx";
$.ajax({
type: 'GET',
url: surl,
crossDomain: true,
contentType: "application/json; charset=utf-8",
data: { UserID: 1234 },
dataType: "jsonp",
success: function(msg) {
$.each(msg, function(name, value) {
alert(value);
});
},
error: function(xhr, status, error) { alert('Servidor de error 404 !!'); },
async: false,
cache: false
});
});
Note:- This will only work with json data type, so while requesting a web service should return the data in Javascript Object notation format.
Revision number 1, Monday, October 03, 2011 7:50:22 PM by vikasrulez
You must Login to comment.
|
Wed, Feb 20, 2013 12:16 AM
by StAyrAw37
|
Hi I am trying to call the Web service using the Above code i am getting a error Service is hosted on http://ServicesHost/LinkService/LinkService.svc/TokenInsertion Application accessing it is https://www.ExampleTry.com code in ajax var service_URL= "http://ServicesHost/LinkService/LinkService.svc/TokenInsertion"; $.ajax({ type: "POST", beforeSend: function (request) { request.setRequestHeader("Date", datetime); request.setRequestHeader("URL", url); }, contentType: "application/json", url: service_URL, data: JSON.stringify(Input), dataType: "jsonp", processData: true, success: function (response) { if (response == "true") { window.open("http://Redirectionsite/EShopApplication"); } else { alert("Access Denied"); } }, error: function (xhr, status, error) { alert(xhr.toString); alert(error); alert(status.toString); } }); it is throwing an error error.description and error.message
jQuery17106403191079096316_1361336745654 was not called status parsererror XHr readystate : 4
status:200
status text :"success"
|