1

a.php

$(document).ready(function() {
    $("#submit_form").on("click",function(){
        var json_hist =  <?php echo $json_history; ?>;
        $.ajax({
            type: "POST",
            url: "b.php",
            data: "hist_json="+JSON.stringify(json_hist),
            //contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(data){alert(data);},
            failure: function(errMsg) {
                alert(errMsg);
            }
        });  
    }); 
})

b.php

$obj=json_decode($_POST["hist_json"]);
var_dump($_POST);

If I comment contentType: "application/json; charset=utf-8" everything's works fine but if uncomment this. The var dump will return null.

3
  • no need to give the json_decode in php file. Commented Jul 11, 2013 at 7:41
  • why you used dataType: "json" ? try to remove it because the output of b.php is not json Commented Jul 11, 2013 at 7:42
  • Thanks but even I don't decode in php file.It return null in var dump. "$history = json_encode($pro_hist);" "var json = <?php echo $history; ?>;" I have encode to json type. But why I don't need to use data Type json ? Thanks Commented Jul 11, 2013 at 7:42

3 Answers 3

1

When you set the contentType in the ajax, you are setting the contentType for the request not the response.

It fails with the JSON contentType because the data you're sending is key/value formatted data (which is missing the encoding) and so the data doesn't match the contentType. The JSON contentType header is for when you're sending raw JSON with no identifiers, but in your case you have an identifier hist_json=.

I suggest changing to:

data: { hist_json : JSON.stringify(json_hist) },

Using an object with the hits_json key will mean that jQuery will safely URL encode the JSON and will allow the PHP to pick it up with $_POST['hits_json'].


If you want to use the JSON contentType then you will have to change the ajax to:

data: { JSON.stringify(json_hist) }, // <-- no identifier

and the PHP:

$obj = json_decode($HTTP_RAW_POST_DATA);
var_dump($obj);
Sign up to request clarification or add additional context in comments.

7 Comments

data: {hits_json:JSON.stringify(json_hist)}, contentType: "application/json; charset=utf-8", dataType: "json", In firebug, the post data hits_json=%7B%22history_done_task%22%3A150%2C%22history_morale%22%3A150%2C%22history_date%22%3A150%2C%22history_skill_lv%22%3A150%7D and the b.php var dump return null. Please help
No, you're using the hist_json identifier, so don't include the JSON contentType. See the answer if you want to use the header, you need to change the ajax and PHP.
It it mean that I do not set the content type everytime I send the json? I would like to learn the most right way. Thanks for help again
Correct, don't send the contentType because your data is not entirely JSON.
Look at the second part of my answer. To send pure JSON you need to remove the hist_json identifier, and change the PHP code.
|
0

From what I know it's bug that appears in FireFox.

you can read more on http://bugs.jquery.com/ticket/13758

There is also topic in stackoverflow about it

Cannot set content-type to 'application/json' in jQuery.ajax

Comments

0

The line you have commented out is trying to change the Content-type: header to application/json. While the data you are sending is in JSON format, the data is being transmitted as an HTTP POST request, so you need to use a content type of application/x-www-form-urlencoded;, which is the default. That's why it works with the line removed.

4 Comments

Is it mean that the most right way of send json do not need to set the content type?
For what you're trying to do here, don't set the content-type.
In which case I should set the content type? Thanks
You might need to set the content type explicitly if you're trying to upload multiple files with Ajax. I can't think of an occasion when you'd need to set it to application/json. Of course, you might want to set the dataType for data that you receive from the server.

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.