从其他服务器Web应用程序调用wcf服务

问题描述:

大家好,

场景是这样的:



A)服务器A正在运行IIS和restfull WCF服务在此IIS上托管



B)服务器B正在运行IIS并且Web应用程序托管在此服务器上

(HTML ,css和Jquery)



现在测试1)当我从服务器B调用网页时,从服务器内部调用服务方法一个wcf服务。

- 在这种情况下,服务调用显示错误状态代码为'0',消息显示为'undefined'



这是一个调用服务的代码:

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 :

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





这里有什么问题?

是否需要任何防火墙设置?

如何使此服务呼叫成功?

请建议,优先!!!



感谢您的提前建议:)



--Avinash



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

.ajax({
type: POST
url: http ://Service1.svc/Registration
data:jsonText2,
contentType: application / json; charset = utf-8
dataType: json
processData: true
timeout: 1000 * 30
成功:功能(数据){
alert( 已注册的succ essfully跨度>);
}
},
错误: function (错误){

alert(error.Message +
+ error.status);
}
});
.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); } });





这里有什么问题?

是否需要任何防火墙设置?

如何使此服务呼叫成功?

请建议,优先!!!



感谢您的提前建议:)



--Avinash



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


这是我的解决方案:

- 通过使用XMLHttpRequest连接到跨域wcf服务。

- 使用xhr.send()方法传递参数。



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);
                     //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);
}





***需要在服务器上添加服务标题:

名称:价值

--------------------------------------- -----------------

Access-Control-Allow-Credentials:true

Access-Control-Allow-Headers:内容类型

内容类型:application / json

访问控制 - 允许 - 方法:获取,发布,选项

访问控制-Allow-Origin:*

Access-Control-Max-Age:100000

------------------- --------------------------------------

这些标题需要在您的服务的web.config文件中设置。

或者您可以使用IIS来设置这些标头。在IIS的功能视图中,您将看到需要设置这些标头的Http Response Headers。



终于看到了成功的消息!!!! Hip Hip Hurray ..... :)



--Avinash



*** 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


这绝对与跨域呼叫有关。



在跨域调用中,如果您的WCF服务器托管在与您的Web应用程序不同的域中,则由于安全策略,您不能发出请求。但是,javascript有一个例外。而且,这就是CDN从我们可以获取脚本的方式,即使托管在不同的域中。



有两种方法可以解决这个问题 -

1.如上所述,使用CORS。



或,

2.使用JSONP代替JSON,详情请见参考 - http://en.wikipedia.org/wiki/JSONP [ ^ ]

这将允许您从其他域注入脚本。但是,当我们使用JSONP时,请求仅限于GET请求。我们不能发出其他类型的请求,如POST,PUT等。



这可以帮助你构建你的jQuery.ajax()函数 -

http://*.com/questions/5943630/basic-example-of -using-ajax-with-jsonp [ ^ ]



我希望这会有所帮助。

谢谢
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://*.com/questions/5943630/basic-example-of-using-ajax-with-jsonp[^]

I hope this will help.
Thanks