Reputation: 1748
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
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);
}
});
}
Upvotes: 1
Views: 754
Reputation: 27364
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));
}
});
Upvotes: 1