Am I sending the JSON object correctly via Ajax?

Well, lets take a moment to pause that debate and go back to the actual method, because i’ve made a misstatement.
dataType defines the RETURN type that the ajax is expecting, not the SEND type.

The data element works just fine with a fully complex Object (of any type), a String, or an Array, as defined by the jQuery specification. I will assume it does some internal conversion to format its send correctly to the server.
EDIT: Not fully complex object. PlainObject (Which… I take to mean a JSON-able object)

The problem is, then you end up with a request sent as application/json, but this as the request body:
sendFormData=%7B%22wark%22%3A%7B%22imajson%22%3A%22doot%22%7D%7D

PHP doesn’t seem to know what to do with this, as in my tests it still doesn’t populate the $_POST array (probably it doesn’t parse the request body into superglobals unless it is sent with a relevant mime type).

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$.ajax({
    url: 'http://127.0.0.1/test2.php',
    type: 'POST',
    data: {'wark':JSON.stringify({'imajson':"doot"})},
    dataType: 'json',
    success: function(msg) {
        console.log(msg);
    }
})
</script>
<?php
echo json_encode(json_decode($_POST['wark']));
?>

Still works absolutely fine for me. (The errors are borking from a browser extension trying to run on localhost… I’m on a different PC now.)

I think you misread what I said in my previous post - I’m talking about when you send it with a mime type of application/json.

You can, of course, submit data (via Ajax, or via a standard HTML form) where one or more fields contain a JSON string. You could then access this string via the $_POST array within your PHP script, and decode the JSON string into PHP from there.

However, the most common way of sending JSON to a server is via a pure application/json request, as the OP seemed to be aiming to do.

OH. Sorry yes i see what you’re saying now.
the contentType should not be application/json, because the server is not a JSON-receiving API system.

Well it only depends on how you code your backend really. If you use the method in post #16 then PHP is just as capable of dealing with pure JSON requests as any other language :wink:

Well yes, but by that logic you can use the method in post #16 for ANY input, it’s just more complex to do so than just… sending the data like most other form data in the world.

Thanks @m_hutley for your great effort in helping me. My problem is finally solved. Thanks @fretburner to you as well for your help.

I don’t use jQuery and I may be wrong but json is a formatted string so maybe you should change data:({title:projectTitle})
to data:(“{title:projectTitle}”)
If in your php you are receiving an empty $_POST array it indicates that your POST request succeeded but that you are sending nothing.
Have you checked for any error messages in the console?

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.