1

我在 jQuery AJAX POST 中调用了一个 REST 服务。当我在 AJAX 调用中不使用beforeSend函数时,该服务被调用并且 http 请求变为POST请求。但是当使用 beforeSend 函数时,请求作为OPTIONS请求传递,服务没有被调用并返回 null。它适用于 IE8。添加beforeSend后 Firefox 中会发生什么?我的代码中的任何错误。 jQuery代码

var postCall = function () {
$.support.cors = true;
var HFAssoRefId = document.getElementById('MainContent_HFAssoRefId').value;
var HFLoginId = document.getElementById('MainContent_HFLoginId').value;
var HFPartnerId = document.getElementById('MainContent_HFPartnerId').value;
var HFFirstName = document.getElementById('MainContent_HFFirstName').value;
var HFLastName = document.getElementById('MainContent_HFLastName').value;
var HFComments = document.getElementById('MainContent_HFComments').value;
var HFCreatedDate = document.getElementById('MainContent_HFCreatedDate').value;
var HFToken = document.getElementById('MainContent_HFToken').value;
var Input = {
AssoRefId: HFAssoRefId, LoginId: HFLoginId, 
PartnerId: HFPartnerId, FirstName: HFFirstName,
LastName: HFLastName, Comments: HFComments,
CreatedDate: HFCreatedDate, Token: HFToken
;
alert(JSON.stringify(Input));
var url = document.URL;
var currentdate = new Date();
var datetime = (currentdate.getMonth() + 1) + "/"
+ currentdate.getDate() + "/"
+ currentdate.getFullYear() + " "
+ currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
$.ajax({
type: "POST",
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("Date", datetime);
xhr.setRequestHeader("URL", url);
},
url: "http://localhost:40680/LinkService.svc/TokenInsertion",        
data: JSON.stringify(Input),
contentType: "application/json",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (xhr, status, error) {
alert(error);
alert(status);
}
});
}
4

1 回答 1

1

这是一个很好的例子。

$.ajax(
{
    type:"POST",
    beforeSend: function (request)
    {
        request.setRequestHeader("Authority", authorizationToken);
    },
    url: "entities",
    data: "json=" + escape(JSON.stringify(createRequestObject)),
    processData: false,
    success: function(msg)
    {
        $("#results").append("The result =" + StringifyPretty(msg));
    }
});
于 2013-02-20T09:53:53.820 回答