Last active
December 21, 2015 06:08
-
-
Save moppa/6261500 to your computer and use it in GitHub Desktop.
jQuery & JSON-P.
Lägg till ?callback=?
This file contains 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
===Hämta=== | |
$j.getJSON("http://headers.jsontest.com/" + "?callback=?", null, function(data) { | |
console.log(data); | |
}); | |
===Skicka=== | |
$j.ajax({ | |
type: 'POST', | |
url: 'http://tapir.haninge.kth.se/~moppa/netflix.php', | |
crossDomain: true, | |
data: '{"some":"json"}', | |
dataType: 'json', | |
success: function(responseData, textStatus, jqXHR) { | |
console.log(responseData); | |
}, | |
error: function (responseData, textStatus, errorThrown) { | |
alert('POST failed.'); | |
} | |
}); | |
===Server=== | |
switch ($_SERVER['HTTP_ORIGIN']) { | |
case 'http://movies2.netflix.com': case 'http://movies.netflix.com': | |
header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']); | |
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); | |
header('Access-Control-Max-Age: 1000'); | |
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With'); | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment