我正在開發使用javascript,html,css(跨平臺技術)的移動應用程序,我已經使用asp.net寫了一個web服務,我想要獲取數據從Web服務和顯示到客戶端使用JavaScript/jQuery的。我們指向Web服務並顯示結果,但這僅適用於IE(Internet Explorer),我們從服務器獲得結果爲「真實」響應,但在其他瀏覽器(Mozilla,Chrome)中不起作用,我們得到結果「假「作爲服務器的響應。因爲我期待結果在所有瀏覽器中都是」真實的「,但它並沒有發生。下面我給出了我用過的所有代碼。如何從跨域使用ajax調用asp.net web服務
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網頁代碼
<!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服務不能理解爲什麼它返回錯誤
請根據問題提供幫助 –