我有一项接受多个参数并安排工作的服务。有些参数可能太长,因此我决定使用POST请求。 服务定义如下:
<WebInvoke(Method:="POST", UriTemplate:="scheduleJob?JobDateTime={JobDateTime}&JobName={JobName}&TestCases={TestCases}&IDs={IDs}&UserName={UserName}", ResponseFormat:=WebMessageFormat.Json)>
Function ScheduleJob(ByVal JobDateTime As String, ByVal JobName As String, ByVal TestCases As String, ByVal IDs As String, ByVal UserName As String) As String
jQuery调用是:
$.ajax({
type: "POST",
crossDomain: true,
url: svcUrl + "jobDateTime={" + jobDateTime + "}jobName={" + jobName + "}TestCases={" + TestCases + "}IDs={" + IDs+ "} + UserName={" + UserName + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data) {
alert("success...");
},
error: function (xhr) {
alert(xhr.responseText);
}
});
在通过fiddler检查时,我发现IE浏览器发送的请求会在标头中发送POST,但是当我使用Mozilla或Chrome时,OPTIONS标头似乎会附加到请求中。
我阅读了一些帖子并对我的asp.net进行了一些修改:
If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "POST,GET")
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "172800")
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")
End If
但似乎没有任何效果。如果我在fiddler中只用“POST”来模拟相同的请求,那么事情似乎很酷。唯一的问题是如何摆脱“选项”?
我获得了OPTIONS请求的405响应代码。标题是:
OPTIONS http://localhost:60403/TaskScheduler.svc/scheduleJob?jobDateTime={12/31/2014%2016:25}jobName={}TestCases={format,localeFormat,_toFormattedString,}Ids={0,}%20+%20UserName={abcd} HTTP/1.1
Host: localhost:60403
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://localhost:54249
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
非常感谢任何其他建议。