Answer the question
In order to leave comments, you need to log in
How to get and process JSON in PHP?
I am writing an ajax post request to another server\domain with json data.
jQuery.ajax({
url: url,
type: 'POST',
data: { action: "get_genres" },
dataType: 'json',
contentType: 'application/json',
json: true
})
.done(function(data){ console.log(data); });
switch (json_decode($_POST['action'])) { case 'get_genres': get_genres();break; }
if(!empty($_POST['data'])) { echo json_encode('true'); } else { echo json_encode('false'); }
if(!empty($_POST['action'])) { echo json_encode('true'); } else { echo json_encode('false'); }
Answer the question
In order to leave comments, you need to log in
Since you want json - then send json:
And on the server instead
it will be
data: JSON.stringify({ action: 'get_genres' })
json_decode($_POST['action'])
json_decode(file_get_contents('php://input'), true)['action']
jQuery.ajax({
url: url,
type: 'POST',
data: { action: "get_genres" },
dataType: 'json'
})
.done(function(data){ console.log(data); });
switch ($_POST['action']) {
case 'get_genres':
get_genres();
break;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question