Skip to content

Instantly share code, notes, and snippets.

@sophiaphillipa
Last active January 29, 2016 16:34
Show Gist options
  • Save sophiaphillipa/9dbd7220321e98b01857 to your computer and use it in GitHub Desktop.
Save sophiaphillipa/9dbd7220321e98b01857 to your computer and use it in GitHub Desktop.
Cross Browser Request with JSONP and PHP
$.ajax({
type: 'GET',
url: 'jsonpAnswer.php',
data: {
form: $(this).serialize()
},
dataType: 'jsonp',
crossDomain: true,
}).done(function(response){
switch(response.type){
case "invalidArgumentError" :
break;
case "internalError" :
break;
case "success":
break;
}
}).fail(function(error){
console.log(error.statusText);
});
//And server side answer
<?php
//Answer must be javascript application type
header('Content-type: application/x-javascript');
$formData = filter_input(INPUT_GET, "form", FILTER_DEFAULT);
//to parse serialized information
parse_str($formData, $formFieldsCollection);
//Make decisions I know this is hard stuff
if(empty($formFieldsCollection['teste'])){
echo $_GET['callback'] . '(' . "{'type' : 'invalidArgumentError' , 'response' : 'Argument not passed mother fucker!' }" . ')';
exit; //Or return, or whatelse your framework could do
}else{
echo $_GET['callback'] . '(' . "{'type' : 'success' , 'response' : 'You was great doind this' }" . ')';
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment