我想開發一個使用VS2005和IIS6的Ajax Webservice。它的目的是查詢一個數據庫,並根據過濾的MS Access Table以text/csv(不是我的選擇!)的形式返回記錄。該服務必須可以從任何客戶端瀏覽器訪問(假設它支持Ajax和XMLHTTPRequest對象),而最有可能的是Linux服務器。這顯然涉及跨站點HTTP請求的現象。當客戶端是MS IE 8時,它可以毫不費力地工作,這可能是因爲Web Tool的slapdash實現了安全性。問題出現在Firefox 12中,其中調用服務的函數返回HTTP/1.1 403 Forbidden。 Web服務代碼,在VB如下:Scrptservice Web服務在MS VS 2005和HTTP POST/CORS/Firefox
Option Explicit On
Option Strict On
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.Odbc
<System.Web.Services.WebService(Namespace:="http://chpt.co.uk/CastusDataTransport")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> <ScriptService()> _
Public Class TransferToolData
Inherits System.Web.Services.WebService
Const AccConStr As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\inetpub\wwwroot\Castus_Server\App_Data\db1.mdb"
<WebMethod()> _
Public Function GetJobRecords(ByVal ClientID As Long) As String
LogGrabJobData("Request for Job Data")
Return GrabJobData(ClientID)
End Function
Private Function GrabJobData(ByVal ID As Long) As String
Dim strData As String
Dim MyDB As New OleDbConnection
Dim drGet As OleDbDataReader
Dim cmdRetrieve As New OleDbCommand
Dim cs As String
Dim RecCount As Long
cs = AccConStr
MyDB = New OleDbConnection(cs)
cmdRetrieve.Connection = MyDB
cmdRetrieve.CommandText = "SELECT * FROM Job_Data WHERE Client_ID = " & ID.ToString
cmdRetrieve.Connection.Open()
drGet = cmdRetrieve.ExecuteReader
strData = "’Job_ID’, ’Client_ID’, ’Status_ID’, ’Product_ID’, ’Serial_No’, ’Date_IN’, ’RA_Scale’, ’Location’, ’Manager’, ’Operator’, ’Stage’\n"
RecCount = 0
Do While (drGet.Read())
strData &= Convert.ToString(drGet.Item("Job_ID")) & ","
strData &= Convert.ToString(drGet.Item("Client_ID")) & ","
strData &= Convert.ToString(drGet.Item("Status_ID")) & ","
strData &= Convert.ToString(drGet.Item("Product_ID")) & ","
'strData &= DelimitTextQuotes(Convert.ToString(drGet.Item("Client_Ref"))) & ","
strData &= DelimitTextQuotes(Convert.ToString(drGet.Item("Serial_No"))) & ","
strData &= Format(drGet.Item("Date_IN"), "d") & ","
'strData &= DelimitTextQuotes(Convert.ToString(drGet.Item("Notes"))) & ","
strData &= Convert.ToString(drGet.Item("RA_Scale")) & ","
strData &= DelimitTextQuotes(Convert.ToString(drGet.Item("Location"))) & ","
strData &= DelimitTextQuotes(Convert.ToString(drGet.Item("Manager"))) & ","
strData &= DelimitTextQuotes(Convert.ToString(drGet.Item("Operator"))) & ","
strData &= Convert.ToString(drGet.Item("Stage")) & "\n"
RecCount += 1
Loop
Return strData
End Function
和客戶端調用它最行人路:
function makeCall(){
var xmlHttp;
try {
xmlHttp = new XMLHttpRequest();
} catch (e) {
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("This sample only works in browsers with AJAX support");
return false;
}
}
}
// Create result handler
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState == 4){
document.getElementById("result").value = xmlHttp.responseText;
}
}
var url;
url = "http://chpt.co.uk/Castus_Server/TransferToolData.asmx/GetJobRecords";
var body = '{"ClientID":';
body = body + document.getElementById("num1").value + '}';
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(body);
}
的403錯誤似乎都來自客戶的預檢要求(我注意到,IE瀏覽器只是跳過 - 糟糕的設計錯誤,我希望其他瀏覽器現在可以自行降低)。 IIS Web應用程序配置爲返回下面的HTTP頭:
Access-Control-Allow-Origin "*"
Access-Control-Allow-Headers Origin,cache-control,man,messagetype,x-requested-with
Access-Control-Allow-Methods POST,OPTIONS
Firefox瀏覽器/服務器發送以下標題:
Request URL:
http://chpt.co.uk/Castus_Server/TransferToolData.asmx/GetJobRecords
Request Method:
OPTIONS
Status Code:
HTTP/1.1 403 Forbidden
Request Headers
15:05:03.984
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-gb,en;q=0.5
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:chpt.co.uk
Origin:null
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 5.1; rv:12.0) Gecko/20100101 Firefox/12.0
Response Headers
?1984ms
Access-Control-Allow-Headers:Origin,cache-control,man,messagetype,x-requested-with
Access-Control-Allow-Methods:POST
Access-Control-Allow-Origin:*
Access-Control-Max-Age:120
Content-Length:1827
Content-Type:text/html
Date:Thu, 17 May 2012 14:04:54 GMT
MicrosoftOfficeWebServer:5.0_PubServer:Microsoft-IIS/6.0
X-Powered-By:ASP.NET
現在我已閱讀關於這一問題的「聖經」的網頁,[https://developer.mozilla.org/en/http_access_control#Access-Control-Allow-Methods] [1],並已經學到了一些東西,但不是解決我目前的問題。只是想知道是否有人能夠看到我出錯的地方,或者指向正確的方向。希望我沒有厭倦每個人,並且我已經提供了足夠的細節來處理我的困境。