1 var sURL= "http://api.xxxxxx.com/services/feeds/
2 xxxxxxx?format=json&callback=?";
3 $.getJSON(sURL, function(data) {
4 // do something with the JSON data returned
5 }); // end get
絀跺叾鍘熷洜錛屽彂鐜皍rl閲屾湁涓猚allback錛岃繖閲屼嬌鐢ㄧ殑鏄疛SONP錛屼竴縐嶈法鍩熸妧鏈?br />緗戜笂鎼滀簡涓婮SONP錛屽鏁伴兘鏄湪璁插浣曚嬌鐢紝鑰屾病鏈夎鏄庝負浠涔堝畠鑳借法鍩燂紝涓旀病鏈夎鏄巙rl鏈嶅姟鍣ㄧ鐨勪唬鐮佹椂濡備綍鐢熸垚鐨勩?br />榪欐柟闈紝涓婇潰鐨勬枃绔犻噷鏈夋瘮杈冭緇嗙殑鎻忚堪銆?br />
1 function jsonp(url,callback,name, query)
2 {
3 if (url.indexOf("?") > -1)
4 url += "&jsonp="
5 else
6 url += "?jsonp="
7 url += name + "&";
8 if (query)
9 url += encodeURIComponent(query) + "&";
10 url += new Date().getTime().toString(); // prevent caching
11
12 var script = document.createElement("script");
13 script.setAttribute("src",url);
14 script.setAttribute("type","text/javascript");
15 document.body.appendChild(script);
16 }
瀹㈡埛绔殑javascript浠g爜浼氭湁濡備笂鐨勬搷浣滐紝浠g爜涓嶉毦錛屽緢瀹規(guī)槗鐞嗚В錛屼篃灝辨槸鍔犱簡涓涓猻cript鏍囩銆?br />
鑰屾湇鍔″櫒绔細榪斿洖callbackFunction( { "x": 10, "y": 15} )錛?/span>
榪欐牱鐨勬暟鎹?span style="color: #000000; font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: none; ">榪欏叾瀹炴槸涓孌礿avascript浠g爜錛?div>callbackFunction鏄柟娉曞悕錛?div>{ "x": 10, "y": 15}鏄弬鏁般傚鎴風(fēng)浠g爜閲屽繀欏昏鏈?div>callbackFunction鏂規(guī)硶鐨勫畾涔夈?/span>
1 public partial class JSONP : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 if (!string.IsNullOrEmpty(Request.QueryString["jsonp"]) )
6 this.JsonPCallback();
7 }
8
9 public void JsonPCallback()
10 {
11 string Callback = Request.QueryString["jsonp"];
12 if (!string.IsNullOrEmpty(Callback))
13 {
14 // *** Do whatever you need
15 Response.Write(Callback + "( {\"x\":10 , \"y\":100} );");
16 }
17
18 Response.End();
19 }
20 }
涔嬪悗瀹㈡埛绔細璋冪敤callbackFunction銆?br />浣跨敤JQuery鐨刧etJSON鏂規(guī)硶鍚庯紝callback閮ㄥ垎浼氬彉鎴愬涓嬪艦寮忋?br />callback=jQuery1710461701650187642_1326201333794&_=1326201356534
callbackFunction鑷劧灝辨槸jQuery1710461701650187642_1326201333794閮ㄥ垎浜嗐?br />榪斿洖鏉ョ殑鏁版嵁涔熶細鏄?span style="color: #000000; font-family: verdana, sans-serif; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; display: inline !important; float: none; ">jQuery1710461701650187642_1326201333794( { "x": 10, "y": 15} ) 榪欑褰㈠紡銆?br />
鏈鍚嶫Query浼氳皟鐢ㄥ尶鍚嶅嚱鏁?div>function(data)錛宒ata鍗充負json鏁版嵁銆?br />
JQuery宸茬粡灝嗗鎴風(fēng)閮ㄥ垎瀹炵幇浜嗭紝鏈嶅姟鍣ㄧ涔熷緱閬靛驚鍚屾牱鐨勮鍒欐墠鑳藉疄鐜拌法鍩熴?br />
*榪欓噷鏈変竴鐐癸紝JQuery濡備綍灝嗗尶鍚嶅嚱鏁版崲鍚嶅瓧鐨勶紵鏈夊摢浣嶅ぇ渚犵煡閬撶殑錛岃鐢ㄧ畝鍗曠殑璇█璁茶В涓嬨?br />

]]>