JS Original Ecological Writing
The code is as follows |
Copy Code |
<script type= "Text/javascript" > function Urljson (o) { O=eval (' (' +o+ ') '); for (var i in O) { Alert (i+ ":" +o);//loop output a:1,b:2,etc. } } var jsonp=document.createelement ("script"); Jsonp.type= "Text/javascript"; Jsonp.src= "Http://www.a.com/1/a.txt"; document.getElementsByTagName ("Head") [0].appendchild (JSONP);//call JSON data across domains Urljson (' {msg: ' is JSON data '} ');//such as native data, this Urljson function </script> |
Remote End
The code is as follows |
Copy Code |
Urljson ({msg: ' This is JSON data '})
|
Urljson local name and remote consistency can be dynamically generated
or below (
JavaScript links must be below the function.
)
The code is as follows |
Copy Code |
<meta content= "text/html; Charset=utf-8 "http-equiv=" Content-type "/> <script type= "Text/javascript" > function Jsonpcallback (Result) { alert (RESULT.A); alert (result.b); alert (RESULT.C); for (var i on result) { Alert (i+ ":" +result);//loop output a:1,b:2,etc. } } </script> <script type= "Text/javascript" src= "Http://www.a.com/1/a.txt" ></script> |
Jquery. Getjson cross-domain Call data
Method definition: Jquery.getjson (URL, data, callback)
Get JSON data by getting request
URL The address page used to provide JSON data
data (Optional) is used to transfer key value pairs to the server
Callback (Optional) callback function, the processing function after the JSON data request succeeds
The code is as follows |
Copy Code |
function (data, textstatus) { Data is a JSON object Textstatus would be "success" This The options for this AJAX request }
|
(1) An object
The code is as follows |
Copy Code |
$.getjson ( "Webdata/json_1.ashx", function (data) { $ ("#divmessage"). Text (data. CustomerName); } );
|
Requests JSON data to the JSON_1.ASHX address, and after receiving the data, processes the data in the function. The data here is a record that corresponds to a customer instance in which the data exists in k/v form. That is, in the form of a [Object,object] array.
{"Unid": 1, "CustomerName": "Song Jiang", "Memo": "Days Kuixing", "other": "Black Three Lang"}
So at the time of the visit, use data. property to access, the following is a k/v loop to print the record of Song Jiang:
The code is as follows |
Copy Code |
$.getjson ( "Webdata/json_1.ashx", function (data) { var tt= ""; $.each (data, function (k, v) { tt + + k + ":" + V + "<br/>"; }) $ ("#divmessage"). HTML (TT); });
|
Results:
Unid:1
CustomerName: Song Jiang
Memo: Day kuixing
Other: Black Tri-lang
(2) Array of objects
Cases
The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <title> Test Documentation </title> <script type= "Text/javascript" src= "/js/jquery-1.4.2.min.js" ></script>
<body> <script language= "javascript" type= "Text/javascript" > function Getjs () { $.getjson ("aaa.php?module=yes&rid=" +math.random (), {Ids:[111,222,333,444,555].join (",")},function (data) { for (Var i=0;i<data.length;i++) { alert (data[i].id); } }); } </script> <input type= "button" value= "Test" onclick= "Javascript:getjs ();" /> </body>
<?php $ids =$_get["IDs"]; $ids =explode (', ', $ids); //$arr =array ("name" => "name", "Age" =>20, ' A ' = > $ids); $arr =array (); $arr []=array (' id ' => $ids [0], ' price_now ' => ' 0.00 ', ' Time_now ' => ' 161427 ', ' u_name_now ' => ' abc ', ' State ' => ' 1 ', ' biddisabled ' => ' 0 '); $arr []=array (' id ' => $ids [1], ' Price_now ' => ' 0.00 ', ' Time_now ' => ' 161427 ', ' u_name_now ' => ' abc ', ' State ' => ' 1 ', ' biddisabled ' => ' 0 '); $arr []=array (' id ' => $ids [2], ' Price_now ' => ' 0.00 ', ' Time_now ' => ' 161427 ', ' u_name_now ' => ' abc ', ' State ' => ' 1 ', ' biddisabled ' => ' 0 '); $arr []=array (' id ' => $ids [3], ' Price_now ' => ' 0.00 ', ' Time_now ' => ' 161427 ', ' u_name_now ' => ' abc ', ' State ' => ' 1 ', ' biddisabled ' => ' 0 '); $arr []=array (' id ' => $ids [4], ' Price_now ' => ' 0.00 ', ' Time_now ' => ' 161427 ', ' u_name_now ' => ' abc ', ' State ' => ' 1 ', ' biddisabled ' => ' 0 '); $jarr =json_encode ($arr); Echo $jarr; ? |