Created
January 1, 2012 14:02
-
-
Save pithyless/1547408 to your computer and use it in GitHub Desktop.
jQuery set Headers for $.ajax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// jQuery Headers support for $.ajax | |
$.ajax({ | |
beforeSend: function(xhrObj){ | |
xhrObj.setRequestHeader("Content-Type","application/json"); | |
xhrObj.setRequestHeader("Accept","application/json"); | |
} | |
type: "POST", | |
url: "/article", | |
processData: false, | |
data: jsonData, | |
dataType: "json", | |
success: function(json){ | |
do something... | |
} | |
}); |
For me it work like this :)
apiReq = $.ajax({
url : APP.apiUrl + path,
method : method || 'get',
//cache : false,
crossDomain: true,
contentType: contentType || 'application/json',
dataType : 'json',
processData: false,
data : values || null,
success : success || APP.onSuccess,
error : error || APP.apiError
});
I'm trying this:
$.ajax({
headers: {
Accept : "text/javascript; charset=utf-8",
"Content-Type": "text/javascript; charset=utf-8"
},
type: "GET", url: rootURL, dataType: "jsonp",
beforeSend: function(xhr){
xhr.setRequestHeader('Access-Control-Allow-origin', 'true');
},
//jsonp: false,
//jsonpCallback: 'eval',
success: function(data) {
console.log(data);
},
error: function(jqXHR, msg, erro){
console.log(erro);
}
});
But the request is going:
Accept: /
HELP!!
hello i think only the server can set 'Access-Control-Allow-origin' @lazaropj
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cannot properly set the Accept HTTP header with jQuery
http://stackoverflow.com/questions/1145588/cannot-properly-set-the-accept-http-header-with-jquery
Answer:
I also had trouble with this, not just in IE but also in Chrome and Safari using jQuery 1.6.2. This solution appears to work as intended in all browsers I've tried (Chrome, Safari, IE, Firefox).