Last active
January 29, 2016 16:34
-
-
Save sophiaphillipa/9dbd7220321e98b01857 to your computer and use it in GitHub Desktop.
Cross Browser Request with JSONP and PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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