我正在使用 javascript、html、css(跨平台技术)开发移动应用程序,我已经使用 asp .net 编写了一个 web 服务,我想从 web 服务中获取数据并使用 javascript/jquery 显示到客户端。我们指向网络服务并显示结果,但这仅在 IE(Internet Explorer)中有效,我们从服务器获得结果为“真实”响应,但在其他浏览器(Mozilla,chrome)中它不起作用,我们得到结果“ false”作为来自服务器的响应。正如我所期望的那样,结果在所有浏览器中都为“true”,但它没有发生。下面我给出了我使用的所有代码。
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;
}
}
}
网页配置
<?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>
请帮助我从所有浏览器调用此 Web 服务,我无法理解为什么它返回 false