KDOT KDOT - 1 month ago 12
PHP Question

Cannot get response of post request

This is my current Javascript code for sending requests every 5 minutes.

function sendRequest() {
$(document).ready(function() {
console.log($.post('../notify/notify.php', { notify: 1 }).done());
});
}

setInterval(function() {
console.log(sendRequest());
}, 5000);





When I then check the network in the developer tools, the requests are being sent and the responses are being returned as expected. This is my PHP code:

header('Content-Type: application/javascript');

if(isset($_POST['notify']) && $_POST['notfiy'] == "1") {
echo json_encode(array(
'Title' => 'Welcome to our Site',
'Message' => 'You are current recieving notifications.',
'Location' => 'http://example.com/',
'State' => true
),true);
} else {
echo json_encode(array(
'State' => false
), true);
}





However, when I check the console I see that it is a type
Object {readyState: "1"}
and when I then expand it, the expected response in wrapped inside a
responseText
field. However,
console.log()
returns undefined when I append
.responseText
to the end of the
$.post().done()
.

What am I doing wrong? How can I get the response from my PHP file?




Update:

After reviewing the questions, I learned that I had the wrong
header()
declared. The type should be
/json
not
/javascript
. Moreover, the response was not readable because like stated, I was only logging the promise of the request. I needed to add a
function(param)
inside my
done()
function and then use the response inside of that.

Answer

What am I doing wrong?

You are wrapping asynchronous call within console.log(), expecting .responseText or .responseJSON of $.post(), or parameter of callback function of $.post() asynchronous call to be logged at console.

console.log() does not wait on asynchronous call to be made or completed. console.log() will log the jQuery promise object returned $.post()

However, when I check the console I see that it is a type Object {readyState: "1"}

not .responseText or .responseJSON properties of the jxXHR object

Returns: jqXHR

.done() accepts a single function or an array of functions, where you can include console.log() within the function body to log the result of the asynchronous operation $.post() that is set as first argument at function callback. .responseText or .responseJSON is accessible as a property of third argument at .done().

.done(function(data, textStatus, jqxhr) {
  console.log(data, jqxhr.responseJSON)
})

You also set Content-Type headers at php to "application/javascript", though you echo an array from php. headers should be set to "application/json". $.post() accepts a third parameter which is the expected Content-Type or dataType of response; here, "json".

// declare variable `interval` for ability to call `clearInterval(interval)`
var interval; 
function sendRequest() {
    return $.post('../notify/notify.php', { notify: 1 }, "json")
    .done(function(data) {
      console.log(data)
    })
    // handle error
    .fail(function(jqxhr, textStatus, errorThrown) {
      console.log(errorThrown)
    });
}

$().ready(function() {
   sendRequest().then(function() {
     interval = setInterval(sendRequest, 5000);
   })
})