Here is my solution :
- By using XMLHttpRequest connect to cross domain wcf service.
- Pass the parameters using the xhr.send() method.
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
xhr = null;
}
return xhr;
}
function CORSURL() {
var val = document.getElementById('val').value;
var url = "http://Service1.svc/UserRegistration";
var passVal = new Object();
passVal.FirstNmae="Avinash";
passVal.LastName="Patil";
passVal.EmailId="Email";
var jsonText2 = JSON.stringify(passVal) ;
var xhr = createCORSRequest('POST', url);
if (!xhr) {
alert('CORS not supported');
}
xhr.onload = function() {
var text = xhr.responseText;
var result=JSON.Parse(xhr.responseText);
alert(text+"!!!@");
};
xhr.onerror = function() {
alert('there is an error.');
};
xhr.setRequestHeader('Content-Type', 'application/json') ;
xhr.send(jsonText2);
}
*** Need to add an headers for your service on server :
Name : Value
--------------------------------------------------------
Access-Control-Allow-Credentials : true
Access-Control-Allow-Headers : content-type
Content-Type : application/json
Access-Control-Allow-Methods : GET,POST,OPTIONS
Access-Control-Allow-Origin : *
Access-Control-Max-Age : 100000
---------------------------------------------------------
These headers need to set in web.config file of your service.
or you can use IIS to set these headers . In IIS in feature view you will see 'Http Response Headers' where you need to set these headers.
Finally saw success message!!!! Hip Hip Hurray..... :)
--Avinash