Javascript 如何使用ajax从跨域调用asp.net web服务
我正在使用javascript、html、css(跨平台技术)开发移动应用程序,我已经使用asp.net编写了一个web服务,我想从web服务获取数据并使用javascript/jquery显示到客户端。我们指向web服务并显示结果,但这仅在IE(internet explorer)中有效,我们从服务器获得的结果为“true”响应,但在其他浏览器(Mozilla、chrome)中不起作用,我们从服务器获得的响应为“false”。我期望的结果是“true”在所有的浏览器中,但它不会发生。下面我给出了我使用的所有代码 WebService.asmx.csJavascript 如何使用ajax从跨域调用asp.net web服务,javascript,ajax,web-services,cors,Javascript,Ajax,Web Services,Cors,我正在使用javascript、html、css(跨平台技术)开发移动应用程序,我已经使用asp.net编写了一个web服务,我想从web服务获取数据并使用javascript/jquery显示到客户端。我们指向web服务并显示结果,但这仅在IE(internet explorer)中有效,我们从服务器获得的结果为“true”响应,但在其他浏览器(Mozilla、chrome)中不起作用,我们从服务器获得的响应为“false”。我期望的结果是“true”在所有的浏览器中,但它不会发生。下面我给出
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;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
///
///WebService的摘要描述
///
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类WebService:System.Web.Services.WebService{
[网络方法]
公共bool GetValue(字符串id,字符串pwd)
{
字符串userid=“abc”;
字符串密码=“xyz”;
if(userid==id&&password==pwd)
{
返回true;
}
其他的
{
返回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页面代码
<!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>
函数JsonTest2(){
jQuery.support.cors=true;
$.ajax({
键入:“POST”,
url:“http://10.16.10.35/webservice_test/WebService.asmx/GetValue",
数据:“{”id:“vipul”,“pwd:“borole”}”,
contentType:'application/json;charset=UTF-8',
数据类型:“json”,
async:false,
成功:功能(msg){
警报(msg.d);
},
错误:函数(msg){
警报(“故障”);
警报(msg);
}
});
}
请帮助我从所有浏览器调用此web服务。我无法理解为什么它返回false您还需要为帖子提供访问控制允许方法 另外,您是否意识到使用*for Origin允许任何人访问该服务?您可能希望使用适合于实现CORS的库,例如ThinkStructure IdentityModel库:
查看IIS/Url选项(因为您的主机似乎不在IIS中)。请根据问题提供帮助