3 回答
TA贡献1877条经验 获得超1个赞
前言:
XMLHTTPRequest
<script>
<head>
JSON请求:
var xhr = new XMLHttpRequest();xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { // success };};xhr.open("GET", "somewhere.php", true);xhr.send();
JSONP请求:
var tag = document.createElement("script");tag.src = 'somewhere_else.php?callback=foo';document.getElementsByTagName("head")[0]. appendChild(tag);
杰森:
{ "bar": "baz" }
JSONP:
foo( { "bar": "baz" } );
callback
<script>
&callback=?
success
?
&callback=?
类似的JSON/JSONP实现
{ "bar" : "baz" }
杰森:
var xhr = new XMLHttpRequest();xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { document.getElementById("output").innerHTML = eval('(' + this.responseText + ')').bar; };};xhr.open("GET", "somewhere.php", true);xhr.send();
JSONP:
function foo(response) { document.getElementById("output").innerHTML = response.bar;};var tag = document.createElement("script");tag.src = 'somewhere_else.php? callback=foo';document.getElementsByTagName("head")[0].appendChild(tag);
- 3 回答
- 0 关注
- 577 浏览
添加回答
举报