beforeSend:AJAX jQuery POST在Firefox中没有传递头值

时间:2022-08-23 17:56:01

I called a REST service in jQuery AJAX POST. The service is called and the http request becomes POST request when I dont use the beforeSend function in AJAX call. But when the beforeSend function is used, the request is passed as OPTIONS request and the service is not called and return null. It works fine with IE8. What happens in Firefox when the beforeSend is added ?? Any mistake in my code.
jQuery Code

我在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);
}
});
}

1 个解决方案

#1


1  

Here is Great Example.

这是很好的例子。

$.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));
    }
});

#1


1  

Here is Great Example.

这是很好的例子。

$.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));
    }
});