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.

collapsar
  • 17,010
  • 4
  • 35
  • 61
hehetest
  • 19
  • 1
  • 5
  • no need to give the `json_decode` in php file. – Code Lღver Jul 11 '13 at 07:41
  • why you used dataType: "json" ? try to remove it because the output of b.php is not json –  Jul 11 '13 at 07:42
  • Thanks but even I don't decode in php file.It return null in var dump. "$history = json_encode($pro_hist);" "var json = ;" I have encode to json type. But why I don't need to use data Type json ? Thanks – hehetest Jul 11 '13 at 07:42

3 Answers3

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);
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • 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 – hehetest Jul 11 '13 at 07:50
  • 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. – MrCode Jul 11 '13 at 07:51
  • 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 – hehetest Jul 11 '13 at 07:55
  • Correct, don't send the contentType because your data is not entirely JSON. – MrCode Jul 11 '13 at 08:00
  • 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. – MrCode Jul 11 '13 at 08:02
  • a.php data: JSON.stringify(json_hist), contentType: "application/json; charset=utf-8", dataType: "json", b.php var_dump($_POST); it return null. Sorry for asking stupid question But it still return null – hehetest Jul 11 '13 at 08:08
  • Yes, by sending pure JSON you can't use `$_POST`, you have to use `$HTTP_RAW_POST_DATA`. – MrCode Jul 11 '13 at 08:10
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

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
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.

  • Is it mean that the most right way of send json do not need to set the content type? – hehetest Jul 11 '13 at 07:53
  • For what you're trying to do here, don't set the content-type. –  Jul 11 '13 at 07:54
  • In which case I should set the content type? Thanks – hehetest Jul 11 '13 at 07:56
  • 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. –  Jul 11 '13 at 08:02