Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Cross-domain access between AJax and Jsonp

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of cross-domain access between AJax and Jsonp, which is very detailed and has a certain reference value. Friends who are interested must read it!

# AJax of JavaScript

AJAX means "Asynchronous Javascript And XML" (asynchronous JavaScript and XML)

One of the important techniques used in designing AJax is the XMLHttpRequest object.

How to create a XMLHttpRequest object:

Xmlhttp = new ActiveObject ("Microsoft.XMLHTTP"); / / creation method supported by IE browser

Xmlhttp = new XMLHTTPRequest (); / / creation methods supported by browsers such as FireFox,Opera

XMLHttp is a set of API that can transmit or receive XML and other data through http protocol in scripting languages such as Javascript, VbScript, Jscript and so on. Can be used to simulate GET and POST requests for http.

You can determine whether the window.XMLHttpRequest object is available and then create the XMLHttpRequest object.

The following are the properties and usage of the XMLHttpRequest object, pasted, and annotated in great detail.

The description of the XMLHTTPRequest object DEMO jQuery (document) .ready (function () {$.ajax ({type: "get", async: false, url: "http://flightQuery.com/jsonp/flightResult.aspx?code=CA1998", dataType:" jsonp ", jsonp:" callback ", / / passed to the request handler or page The parameter name used to obtain the name of the jsonp callback function (generally default: callback) jsonpCallback: "flightHandler", / / the custom jsonp callback function name, which defaults to the random function name automatically generated by jQuery, or you can write "?". JQuery will automatically process the data success: function (json) {alert ('you found flight information: ticket price:' + json.price + 'CNY). Remaining tickets:'+ json.tickets + 'Zhang. ;, error: function () {alert ('fail');});})

Finally, Ajax and jsonp are two completely different things. The core of ajax is to get non-page content through XmlHttpRequest, while the core of jsonp is to dynamically add script tags to invoke js scripts provided by the server.

How to obtain data across domains through JSONP in jQuery

The first method is to set dataType to 'jsonp': in the ajax function

$.ajax ({dataType: 'jsonp', jsonp:'callback', url:' http://www.a.com/user?id=123', success: function (data) {/ / processing data data}})

The second method is to implement it using getJSON, as long as you add callback=? to the address Parameters are available:

$.getJSON ('http://www.a.com/user?id=123&callback=?', function (data) {/ / processing data data})

You can also simply use the getScript method:

/ / you can also define the foo method function foo (data) {/ / process data data} $.getJSON ('http://www.a.com/user?id=123&callback=foo');) outside the function at this time

Application of JSONP

JSONP can play a very important role in open API. Open API is used on developers' own applications, and many applications are often on developers' servers rather than on Sina Weibo servers. Therefore, cross-domain data request has become a major problem that developers need to solve. The vast open platform should support JSONP. The Sina Weibo open platform does this very well (although it is not stated in some API, it can actually be called using JSONP).

The above is all the content of the article "sample Analysis of AJax and Jsonp Cross-domain access". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report