Example of JSON data for cross-origin javascript calls

Source: Internet
Author: User

If we use the original javascript cross-origin to call JSON data, we use createElement and dynamic to add a remote data interface to src. Now we can also use jquery ajax getjson for an instance.

Original js 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); // cyclically Outputs 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); // JSON data for cross-origin calls
// Urljson ("{msg: 'This is json data'}"); // calls the urljson function like this for local data.

</Script>

Remote End

The Code is as follows: Copy code


Urljson ({msg: 'This is json data '})

The local name of urljson must be the same as that of the remote device, and can be dynamically generated.

Or the following (

The JavaScript link must be under 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. );
Alert (result. B );
Alert (result. c );
For (var I in result ){
Alert (I + ":" + result); // cyclically Outputs a: 1, B: 2, etc.
}
}
</Script>

<Script type = "text/javascript" src = "http://www.a.com/1/a.txt"> </script>


Jquery. getJSON cross-origin call data


Method definition: jQuery. getJSON (url, data, callback)
Get json data through get request
· Url is used to provide the address page of json data
· Data (Optional) is a key-value pair that is sent to the server.
· Callback (Optional) callback function, the processing function after the json data request is successful

The Code is as follows: Copy code
Function (data, textStatus ){
// Data is a json object
// TextStatus will 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 );
}
);
 


Request json data from the Json_1.ashx address. After receiving the data, process the data in the function. The data here is a record, corresponding to a customer instance, where the data exists in k/V format. It exists as an array of [object, object.
{"Unid": 1, "CustomerName": "", "Memo": "", "Other": ""}

Therefore, data. Property is used for access. The following uses a k/v loop to print the song river record:

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" 2.16.html (tt );
});

 
Result:
Unid: 1
CustomerName: Song Jiang
Memo: Tian kuixing
Other: Hei sanlang
(2) object Array

Example

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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> test document </title>
<Script type = "text/javascript" src = "/javascript/jquery-1.4.2.min.js"> </script>
</Head>

<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>
</Html>

 

<? Php
$ Ids = $ _ GET ["ids"];
$ Ids = explode (',', $ ids );
// $ Arr = array ("name" => "name", "age" => 20, 'A' => $ ids );
$ Arr = array ();
$ Arr [] = array ('id' => $ ids [0], 'price _ right' => '0. 00', 'Time _ now '=> '000000', 'U _ name_now' => 'abc', 'state' => '1 ', 'iddisabled '=> '0 ');
$ Arr [] = array ('id' => $ ids [1], 'price _ right' => '0. 00', 'Time _ now '=> '000000', 'U _ name_now' => 'abc', 'state' => '1 ', 'iddisabled '=> '0 ');
$ Arr [] = array ('id' => $ ids [2], 'price _ right' => '0. 00', 'Time _ now '=> '000000', 'U _ name_now' => 'abc', 'state' => '1 ', 'iddisabled '=> '0 ');
$ Arr [] = array ('id' => $ ids [3], 'price _ right' => '0. 00', 'Time _ now '=> '000000', 'U _ name_now' => 'abc', 'state' => '1 ', 'iddisabled '=> '0 ');
$ Arr [] = array ('id' => $ ids [4], 'price _ right' => '0. 00', 'Time _ now '=> '000000', 'U _ name_now' => 'abc', 'state' => '1 ', 'iddisabled '=> '0 ');
$ Jarr = json_encode ($ arr );
Echo $ jarr;
?>

Related Article

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.