{"id":927,"date":"2013-11-14T18:34:48","date_gmt":"2013-11-14T02:34:48","guid":{"rendered":"http:\/\/hesiwei.cn\/?p=927"},"modified":"2013-11-14T18:34:48","modified_gmt":"2013-11-14T02:34:48","slug":"php%e4%b8%ad%e4%bd%bf%e7%94%a8jsonp%e8%b7%a8%e5%9f%9f%e4%bc%a0%e9%80%92%e5%86%85%e5%ae%b9","status":"publish","type":"post","link":"http:\/\/hesiwei.cn\/?p=927","title":{"rendered":"php\u4e2d\u4f7f\u7528jsonp\u8de8\u57df\u4f20\u9012\u5185\u5bb9"},"content":{"rendered":"<div>\u6211\u4eec\u73b0\u5728www.test.com\u8fd9\u4e2a\u57df\u540d\u4e0b\u9762\u6709\u8fd9\u4e48\u4e2ahtml\u6587\u4ef6testjsonp.html\uff1a<\/div>\n<div><\/div>\n<div>&lt;!DOCTYPE html PUBLIC &#8220;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&#8221; &#8220;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&#8221;&gt;<\/div>\n<div>\u00a0&lt;html xmlns=&#8221;http:\/\/www.w3.org\/1999\/xhtml&#8221; &gt;<\/div>\n<div>\u00a0&lt;head&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0&lt;title&gt;Untitled Page&lt;\/title&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 &lt;script type=&#8221;text\/javascript&#8221; src=&#8221;jquery-1.7.2.min.js&#8221;&gt;&lt;\/script&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0 &lt;script type=&#8221;text\/javascript&#8221;&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0jQuery(document).ready(function(){<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 $.ajax({<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0type: &#8220;GET&#8221;,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0async: false,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0url: &#8220;http:\/\/test\/jsonp.php&#8221;,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0dataType: &#8220;jsonp&#8221;,<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0jsonp: &#8220;callback&#8221;,\/\/\u4f20\u9012\u7ed9\u8bf7\u6c42\u5904\u7406\u7a0b\u5e8f\u6216\u9875\u9762\u7684\uff0c\u7528\u4ee5\u83b7\u5f97jsonp\u56de\u8c03\u51fd\u6570\u540d\u7684\u53c2\u6570\u540d(\u4e00\u822c\u9ed8\u8ba4\u4e3a:callback)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0jsonpCallback:&#8221;flightHandler&#8221;,\/\/\u81ea\u5b9a\u4e49\u7684jsonp\u56de\u8c03\u51fd\u6570\u540d\u79f0\uff0c\u9ed8\u8ba4\u4e3ajQuery\u81ea\u52a8\u751f\u6210\u7684\u968f\u673a\u51fd\u6570\u540d\uff0c\u4e5f\u53ef\u4ee5\u5199&#8221;?&#8221;\uff0cjQuery\u4f1a\u81ea\u52a8\u4e3a\u4f60\u5904\u7406\u6570\u636e<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0success: function(json){<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0alert(&#8216;\u60a8\u67e5\u8be2\u5230\u822a\u73ed\u4fe1\u606f\uff1a\u7968\u4ef7\uff1a &#8216; + json.price + &#8216; \u5143\uff0c\u4f59\u7968\uff1a &#8216; + json.tickets + &#8216; \u5f20\u3002\u56de\u8c03\u51fd\u6570\u540d\u4e3a: &#8216;+json.func);<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0},<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0error: function(){<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0alert(&#8220;fail&#8221;);<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0}<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0});<\/div>\n<div>\u00a0 \u00a0 \u00a0});<\/div>\n<div>\u00a0 \u00a0 \u00a0&lt;\/script&gt;<\/div>\n<div>\u00a0 \u00a0 \u00a0&lt;\/head&gt;<\/div>\n<div>\u00a0 &lt;body&gt;<\/div>\n<div>\u00a0 &lt;\/body&gt;<\/div>\n<div>\u00a0&lt;\/html&gt;<\/div>\n<div>\u6ce8\u610f\uff0c\u8981\u771f\u6b63\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\u53ef\u80fd\u9700\u8981jquery\u7684\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u5c06&lt;script type=&#8221;text\/javascript&#8221; src=&#8221;jquery-1.7.2.min.js&#8221;&gt;&lt;\/script&gt;\u6539\u4e3a\u4f60\u76ee\u5f55\u4e2djquery\u7684\u6587\u4ef6\u8def\u5f84\uff1a<\/div>\n<div><\/div>\n<div>\u5982\uff1a&lt;script type=&#8221;text\/javascript&#8221; src=&#8221;js\/jquery.js&#8221;&gt;&lt;\/script&gt;<\/div>\n<div>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u518d\u627e\u4e2a\u53e6\u5916\u4e00\u4e2a\u57df\u540d\u7684web\u76ee\u5f55\uff0c\u5c06\u6587\u4ef6jsonp.php:<\/div>\n<div><\/div>\n<div>&lt;?php<\/div>\n<div><\/div>\n<div>$callback = $_GET[&#8220;callback&#8221;];<\/div>\n<div>$a = array(<\/div>\n<div>\u00a0 \u00a0 &#8216;code&#8217;=&gt;&#8217;CA1998&#8217;,<\/div>\n<div>\u00a0 \u00a0 &#8216;price&#8217;=&gt;&#8217;6000&#8217;,<\/div>\n<div>\u00a0 \u00a0 &#8216;tickets&#8217;=&gt;20,<\/div>\n<div>\u00a0 \u00a0 &#8216;func&#8217;=&gt;$callback,<\/div>\n<div>);<\/div>\n<div>$result = json_encode($a);<\/div>\n<div>echo &#8220;flightHandler($result)&#8221;;<\/div>\n<div>exit;<\/div>\n<div><\/div>\n<div>\u653e\u5230\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u9762\u53bb\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u6d4b\u8bd5\u4e86\u3002<\/div>\n<div>\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u8bbf\u95eetestjsonp.html.\u5c31\u53ef\u4ee5\u770b\u5230\u6548\u679c\u4e86\u3002<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u73b0\u5728www.test.com\u8fd9\u4e2a\u57df\u540d\u4e0b\u9762\u6709\u8fd9\u4e48\u4e2ahtml\u6587\u4ef6testjsonp.html\uff1a &lt;!DO [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts\/927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=927"}],"version-history":[{"count":0,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions"}],"wp:attachment":[{"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hesiwei.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}