Jquery.getjson () Grammar and use method

Source: Internet
Author: User

Jquery.getjson (URL, [data], [Callback (data, Textstatus)]) returns:xmlhttprequest

Urla string containing the URL to which the request is sent.

Dataa map or string is sent to the server with the request.

Callback (data, textstatus) a callback function that is executed if the request succeeds

$.ajax ({
Url:url,
DataType: ' JSON ',
Data:data,
Success:callback
});


The callback is by returning the data, which will be a Web page effect object or the array defines the structure and parsing of JSON using $. Parsejson () method.

Most implementations will specify a successful handler:

$.getjson (' Ajax/test.json ', function (data) {
$ ('. Result '). html (' <p> ' + data.foo + ' </p> ')
+ ' <p> ' + data.baz[1] + ' </p> ');
});


This example, of course, relies on the structure of the JSON on the file:

{
"foo": "The quick brown fox jumps tutorial over the lazy dog."
"Bar": "ABCDEFG",
"Baz": [52, 97]
}


Use this structure, such as inserting the first string, from the second to the number of paging files.

It's important: in jquery 1.4, if the file contains a JSON syntax error, the request usually fails to prompt. Avoid the frequent hand-edited JSON data for this reason. JSON is a data interchange format that is more restrictive than the text symbols of JavaScript objects. For example, all representations in the JSON string, whether property or value, must be enclosed in double quotes. See http://json.org/for details about the JSON format.

Jsonp
If the URL contains a string "callback =?" "In the URL, the request is treated as JSONP instead." See JSONP data type for discussion, $. Ajax () for more details.

Additional considerations:
Due to browser security restrictions, most "Ajax" requirements are based on the same source policy; Requests cannot be successfully retrieved from different domains, subdomains, or protocol data.
Scripts and JSONP requests are not subject to the same restrictions on the origins of the policy

<!doctype html>
<style>img{height:100px; float:left;} </style>
<script src= "Http://code.jquery.com/jquery-latest.min.js" ></script>
<body>
<div id= "Images" >

</div>
<script>$.getjson ("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any& Format=json&jsoncallback=? ",
function (data) {
$.each (Data.items, function (I,item) {
$ ("if (i = = 3) return false;
});
});</script>

</body>

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.