dilipkumar regana

dilipkumar regana

  • 1.6k
  • 90
  • 31.6k

cross domain wcf service using Jquery

Aug 12 2013 9:46 AM

Answers (3)

0
john abraham

john abraham

  • 0
  • 2
  • 0
Aug 23 2013 7:43 AM
you can call it using ajax call. see this link where you can find example of cross-domain service call
0
Avinash Patil

Avinash Patil

  • 0
  • 22
  • 1.2k
Aug 16 2013 5:51 AM
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);
}
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
0
Mahesh Chand

Mahesh Chand

  • 1
  • 269.7k
  • 240.4m
Aug 12 2013 3:04 PM
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.