Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
Scenario is like this :

A) Server A is having IIS running on it and the restfull WCF service is hosted on this IIS

B) Server B is having IIS running on it and the web application is hosted on this server
(HTML,css and Jquery)

Now test 1) When I am calling the web page from server B which internally calling the service methods from server A wcf service.
- In this situation the service call is showing the error status code '0' and the message as 'undefined'

Here is a code to call the service :
JavaScript
 var jsonText2 = JSON.stringify({ "FirstName": fname, "LastName": lname, "EmailId": email, "Password": passwordEnc });
        alert(jsonText2 );
    $.ajax({
        type: "POST",
        url: "http://Service1.svc/Registration",
        data: jsonText2,
        contentType: "application/json;charset=utf-8",
        dataType: "json",
	processData: true,
        timeout: 1000 * 30 ,
        success: function (data) {
                alert("Registered successfully");
            }
        },
		error: function(error) {
 
  alert(error.Message+" "+error.status);
}
    });


what is the issue here?
Is there any firewalls settings are required?
How to make this service call success ?
Please suggest , it priority !!!

Thanks for your suggestion in advance :)

--Avinash
Posted
Comments
Keith Barrow 26-Jul-13 8:29am    
Could be firewall, but the first thing I'd do is download fiddler (http://fiddler2.com/) and inspect the message being sent from the client to the server to ensure the expected message is being sent up. I'd also make sure the fields in jsonText2 / the Http request match those on the service. If you can, get a debugger onto the target service (host locally and amend the JavaScript as necessary) ans find out what is arriving there.

Here is my solution :
- By using XMLHttpRequest connect to cross domain wcf service.
- Pass the parameters using the xhr.send() method.

JavaScript
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;
}


JavaScript
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 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
 
Share this answer
 
v2
This is definitely related to Cross Domain Calls.

In Cross Domain Calls, if your WCF server is hosted in different domain than your web application you are not allowed to make a request due to Security Policies. But, there is an exception with javascript. And, that's how CDN's from where we can get the scripts, even if hosted in different domains.

There are two ways to resolve this -
1. As you are suggested above, use CORS.

or,
2. Use JSONP in place of JSON, for details refer to - http://en.wikipedia.org/wiki/JSONP[^]
This will allow you to inject scripts from another domain. But, when we use JSONP the requests are limited to GET requests only. We cannot make other type of request like POST, PUT etc.

This can help you to frame your jQuery.ajax() function -
http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp[^]

I hope this will help.
Thanks
 
Share this answer
 
v2
As already said by sreejithmv001, that this is due security policy of the browser. To access the services in other hosted in another domain, you need to some changes. This is also known as CORS (Cross-origin resource sharing). I have written a post to handle this scenario
http://brijbhushan.net/2013/02/12/accessing-other-domain-web-services-via-jquery-ajax-cors/[^]
 
Share this answer
 
This is due to browser security policy. Normally call cross domain issue.

I already answered this question in How to post Json data on server in query or ajax[^].

Hope this helps you

Happy Coding :-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900