Here is 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); } elseif (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); //access object values using property name 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 wcf 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. --Avinash
What do you mean by cross domain? If you have a WCF service, it can be available from any application if hosted publicly. So it does not matter from where you call it.
john abrahamPosted Aug 23, 2013, 7:43 AM
Avinash PatilPosted Aug 16, 2013, 5:51 AM
- 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); //access object values using property name
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 wcf 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.
--Avinash
Mahesh ChandPosted Aug 12, 2013, 3:04 PM