Skip to content

Instantly share code, notes, and snippets.

@appastair
Last active August 23, 2020 17:52
Show Gist options
  • Save appastair/3865796 to your computer and use it in GitHub Desktop.
Save appastair/3865796 to your computer and use it in GitHub Desktop.
Cross-domain JSONP Example (jQuery/PHP)
jQuery(function($){
$.ajax({
type: 'GET',
url: '//remote.org/jsonp.php',
data: {
field: 'value'
},
dataType: 'jsonp'
crossDomain: true,
}).done(function(response){
console.log(response);
}).fail(function(error){
console.log(error.statusText);
});
);
<?php
header('Content-type: application/x-javascript');
echo $_GET['callback']."([".json_encode($_GET)."])";
?>
@rickytheferris
Copy link

My question is how to manipulate the variables in PHP? anything I do just seems to break it...

@MikSDigital
Copy link

@samarulrajt
Copy link

1,'email'=>'[email protected]'); echo $_GET['callback'] . '(' . "{'success' : '1' , 'user' : ".json_encode($data)." }" . ')';die(); ?>

@kigh143
Copy link

kigh143 commented Mar 4, 2016

hey , am tying to use the jsonp but it still shows this error <-----because it violates the following Content Security Policy directive--->
anyone please help me

@wotta
Copy link

wotta commented Apr 1, 2016

try to add ?callback=? ( don't know if it will work )

@andibastian
Copy link

thankyou, myproblem solved

@Mutale85
Copy link

I wish to submit that this helped me. I was trying an API i made.

@appastair
Copy link
Author

I'm happy to hear it, @Mutale85 - all the best! 👋 😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment