Reputation: 1748
I called a Rest service in jQuery AJAX POST method. I need to pass two values in header.
Firefox neither pass the header value to the service nor calls the REST service.
My Jquery code
var postCall = function () {
$.support.cors = true;
var HFAssoRefId = document.getElementById('MainContent_HFAssoRefId').value;
var Input = {
AssoRefId: HFAssoRefId
};
alert(JSON.stringify(Input));
var url = document.URL;
var name = "samuel";
$.ajax({
type: "POST",
beforeSend: function (xhr) {
xhr.setRequestHeader("PartnerName", name);
xhr.setRequestHeader("URL", url);
},
url: "http://localhost:40680/Service.svc/TokenInsertion",
data: JSON.stringify(Input),
contentType: "application/json",
dataType: "json",
success: function (response) {
alert(response);
},
error: function (xhr, status, error) {
alert(status);
}
});
}
Is there any other methods to pass the header values in jQuery AJAX. It works fine with Internet Explorer 8. How to make it work compatible with Firefox browser also ?
I tried the other methods for posting like this. Method 1 :
$(document).ready(function () {
$("#button").click(function(){
var name1 = "samuel";
var url1 = document.URL;
$.post('http://localhost:41855/IntegrationCheck/Default.aspx', {
name : name1,
url : url1
},function (data) {
alert(data);
});
});
});
and MEthod 2 in AJAX jQuery:
function setHeader() {
var xhr = new XMLHttpRequest();
xhr.open("POST", url, true);
xhr.setRequestHeader('Accept', '');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Accept", "text/xml;application/json");
xhr.setRequestHeader("PartnerName", name);
xhr.setRequestHeader("URL", url);
}
But the header values are not passed and the service is not called in Firefox. Any suggestions..
Upvotes: 1
Views: 1469
Reputation: 1571
I used to follow the below approach to set request header. Please try it if works for you.
[Script]
$("#element").ajaxSuccess(function (evt, request, settings) {
$('.Status').html(request.getResponseHeader("Status"));
});
[View]
<h2>Status:</h2><h2 class="Status" style="color:Red;">
[Controller]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Save(Order orders)
{
string status = string.Empty;
if (orders != null)
{
OrderRepository.Update(orders);
status = "Updated";
}
Response.AddHeader("Status", status);
return data.GridActions<EditableOrder>();
}
Upvotes: 1
Reputation: 5662
Read about headers at jQuery.ajax.
Pass request headers in a jQuery AJAX GET call
Upvotes: 0