I have a method I’m trying to write that can post data to a php file and get the results and return the output in a variable. For some reason, my block of code is not working.
function post_get(){
var result = null;
$.post("modules/data.php", { "func": "getNameAndTime" },
function(data){
result = JSON.parse(data);
}, "json");
return result;
}
I get this error when using this method
SyntaxError: JSON Parse error: Unexpected identifier “undefined”
If your server returns proper JSON encoded output and sets the correct headers (
Content-Type: application/json), you can usedataimmediately:In fact, even if it didn’t return proper data, the
console.log(data)should provide you with enough information to figure out why it wasn’t working in the first place.