1

I got a problem with the headers when i'm trying to do a POST Request with JSON
This is the code:

$.ajax({
                type: "POST",
                url: url,
                data: jsonData,
                dataType: 'json',
                beforeSend: function(xhrObj){
                    xhrObj.setRequestHeader("Content-Type","application/json");
                    xhrObj.setRequestHeader("Accept","application/json");
                },
                error: function(){
                    alert("Fail");
                },
                success: function(){
                    alert("Success");
                }
            });

And this are the Request Headers displayed by Firebug.

OPTIONS /path HTTP/1.1
Host: 192.168.15.109:8080
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20100101 Firefox/12.0 FirePHP/0.7.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Origin: http://localhost
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
x-insight: activate
Pragma: no-cache
Cache-Control: no-cache

And the Response Headers:

HTTP/1.1 204 No Content
Date: Thu, 24 May 2012 19:17:01 GMT
Allow: OPTIONS,POST

As you can see, the headers doesnt match with the ones im specifying, but when i use CURL the Headers are this ones instead:

POST /path HTTP/1.1
User-Agent: curl/7.25.0 (i386-pc-win32) libcurl/7.25.0 OpenSSL/0.9.8u zlib/1.2

Host: localhost:8080
Accept: */*
Content-Type: application/json
Content-Length: 5

Any idea or solution for this?

I also modified JQuery Source to set default values of the Headers sent by Ajax to JSON, but didnt work.

5
  • What do you get in the console if you log xhrObj within the beforeSend function call? console.log(xhrObj). Does it show that those properties were actually set? Commented May 24, 2012 at 19:58
  • Why do you want to set the "Accept" explicitly? The */* in "Accept" header of Firefox says it can accept response in any format. Refer link - gethifi.com/blog/browser-rest-http-accept-headers Commented May 24, 2012 at 19:59
  • I think perhaps your issue may instead be the way your server is configured may not be issuing the correct header to the response. Commented May 24, 2012 at 20:06
  • @ariestav I got 4 empty variables and the functions that are allowed withing the xhrObj. Commented May 24, 2012 at 20:34
  • @user1169578 But why would it not issue the correct headers? CURL and a JavaClient worked perfectly. And cant be a Cross-Domain problem 'cause everything its on the same server. Commented May 24, 2012 at 20:39

2 Answers 2

1

Seems like a same-origin policy issue. Using dataType='jsonp' should work, but this might require other changes.

See https://developer.mozilla.org/en/http_access_control for an in-depth explanation.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer but the Server doesnt allow GET, so i need to use POST for this an JSONP wont allow me to do this.
I dont think its a CrossDomain problem, everything its running on the same server.
1

Actually, it was a cross Domain problem, I defined my URL as an IP, so the browser interpreted it like a Cross Domain request.

Thanks for everything!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.