$. GetJSON () Principle of cross-Origin

Source: Internet
Author: User

/* Data can be requested across domains */

The principle is to use a global function (callback) to operate the server data loaded by <script>.

First, write a simple example: (jsonpCallback can dynamically create and register as a global function)

<Script type = "text/javascript">
Var m = Math. random ();
Var jsonpCallback = new Function ("result", "alert (result. data );");
</Script>
<Script type = "text/javascript" src = "http: // localhost/json. php? Jsonp = jsonpCallback "> </script>

 

Server:

<? Php
Echo "jsonpCallback ({data: 'json data '})";

 

Json data

 

 

$. Get (url, params, callback)

Like the $. post request method, the request type is different.

Returns the character format. You can use the $. evalJSON () method to convert the format.

Then perform operations on the JSON object

$. GetJSON (url, params, callback)

Return a JSON object. The cross-origin example is as follows:

  1. <? Php

  2. Function getdata ()

  3. {

  4. $. GetJSON (

  5. "Http://www.test.com/payment/payment/paytest? Callback =? ",

  6. {Id: 1, name: "name "},

  7. Function (jsondata ){

  8. Alert (jsondata. json );

  9. });

  10. }

  11. Open the source code of jquery and find it step by step:

  12. GetJSON: function (url, data, callback ){

  13. Return jquery. Get (URL, Data, callback, "JSON ");

  14. }

  15. Find jquery. Get again

  16. Get: function (URL, Data, callback, type ){

  17. // Shift arguments if data argument was omited

  18. If (jquery. isfunction (data )){

  19. Type = type | callback;

  20. Callback = data;

  21. Data = NULL;

  22. }

  23. Return jQuery. ajax ({

  24. Type: "GET ",

  25. Url: url,

  26. Data: data,

  27. Success: callback,

  28. DataType: type

  29. });

  30. }

  31. Find jQuery. ajax again

  32. JQuery. ajax ({

  33. Url: url,

  34. Type: type,

  35. DataType: "html ",

  36. Data: params,

  37. Complete: function (res, status ){

  38. // If successful, inject the HTML into all the matched elements

  39. If (status = "success" | status = "notmodified "){

  40. // See if a selector was specified

  41. Self.html (selector?

  42. // Create a dummy div to hold the results

  43. JQuery ("<div/> ")

  44. // Inject the contents of the document in, removing the scripts

  45. // To avoid any 'permission Denied 'errors in IE

  46. . Append (res. responseText. replace (rscript ,""))

  47. // Locate the specified elements

  48. . Find (selector ):

  49. // If not, just inject the full result

  50. Res. responsetext );

  51. }

  52. If (callback ){

  53. Self. each (callback, [res. responseText, status, res]);

  54. }

  55. }

  56. });

  57. Return this;

  58. }

  59. Find the ajax method and uncover the secret:

  60. Because there are too many posts at the beginning, you can check them if you are interested.

  61. Ajax: function (origSettings ){

  62. Var s = jQuery. extend (true, {}, jQuery. ajaxSettings, origSettings );

  63. Var jsonp, status, data,

  64. CallbackContext = origSettings & origSettings. context | s,

  65. Type = s. type. toUpperCase ();

  66. // Convert data if not already a string

  67. If (s. data & s. processData & typeof s. data! = "String "){

  68. S. data = jQuery. param (s. data, s. traditional );

  69. }

  70. Important part:

  71. Http: // localhost/index/ajax? Callback = jsonp00004438515229 & id = 1

  72. The server determines whether the callback parameter exists. If yes, the system returns the JS function name + object.

  73. // Jsonp = jsonp000044000015229 (callback parameter of the request address)

  74. // Jsonp global function

  75. Window [jsonp] = window [jsonp] | function (tmp ){

  76. Data = tmp;

  77. Success ();

  78. Complete ();

  79. // Garbage collect

  80. Window [jsonp] = undefined;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.