javascript 如何使用ajax从跨域调用asp.net web服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17317374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call asp.net web services using ajax from cross domain
提问by Deepak Borade
i am developing mobile application using javascript,html,css(cross platform technology),i have written a web services using asp .net, i want to fetch the data from web services and display into client side using javascript/jquery. we point to web services and display the result but this is work in IE(internet explorer) only, we get the result as "true" response from server but in other browser (Mozilla,chrome) it does not work,we get result "false" as response from server.where as i am expecting result come as "true" in all the browser but its not happening.below i have given all the code that i have used.
我正在使用 javascript、html、css(跨平台技术)开发移动应用程序,我已经使用 asp .net 编写了一个 web 服务,我想从 web 服务中获取数据并使用 javascript/jquery 显示到客户端。我们指向网络服务并显示结果,但这仅在 IE(互联网浏览器)中有效,我们从服务器获得结果为“真实”响应,但在其他浏览器(Mozilla,chrome)中它不起作用,我们得到结果“ false”作为来自服务器的响应。因为我期望结果在所有浏览器中都为“true”,但它没有发生。下面我给出了我使用的所有代码。
WebService.asmx.cs
WebService.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
[WebMethod]
public bool GetValue(string id,string pwd)
{
string userid = "abc";
string password = "xyz";
if (userid==id && password==pwd)
{
return true;
}
else
{
return false;
}
}
}
Web.config
网页配置
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
HTML page Code
HTML 页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
</title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script>
function JsonTest2() {
jQuery.support.cors = true;
$.ajax({
type: 'POST',
url: "http://10.16.10.35/webservice_test/WebService.asmx/GetValue",
data: '{"id":"vipul","pwd":"borole"}',
contentType: 'application/json; charset=UTF-8',
dataType: 'json',
async: false,
success: function (msg) {
alert(msg.d);
},
error: function (msg) {
alert('failure');
alert(msg);
}
});
}
</script>
</head>
<body>
<input id="Button1" type="button" value="button" onclick="javascript:JsonTest2();" />
</body>
</html>
please help me to call this web service from all the browser i am not able to understand why it returning false
请帮助我从所有浏览器调用此 Web 服务,我无法理解为什么它返回 false
回答by Brock Allen
You need to also have Access-Control-Allow-Methods for the POST.
您还需要为 POST 设置 Access-Control-Allow-Methods。
Also, you realize that using * for Origin allows anyone to access the service? You might want to use a library that's suited for implementing CORS, such as the Thinktecture IdentityModel library:
此外,您是否意识到对 Origin 使用 * 允许任何人访问该服务?您可能想要使用适合实现 CORS 的库,例如 Thinktecture IdentityModel 库:
http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/
http://brockallen.com/2012/06/28/cors-support-in-webapi-mvc-and-iis-with-thinktecture-identitymodel/
Look into the IIS/Url option (since it seems you're hosting out of IIS).
查看 IIS/Url 选项(因为您似乎在 IIS 之外托管)。