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

An example Analysis of data types in Ajax

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

Share

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

Today, I would like to share with you the relevant knowledge points of Ajax's data type example analysis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

In the use of Ajax, we have encountered such a problem: the interface is developed for use on mobile phones, but the data is provided through the API provided by the server.

The Ajax of JQuery is used to call the data. At first, it cannot be called, and it does not report a fault. Later, after the browser has been tested, the local interface is loaded on the mobile phone.

1. The key codes of the server are as follows:

/ * *

* @ see HttpServlet#doGet (HttpServletRequest request, HttpServletResponse)

* response)

, /

Protected void doGet (HttpServletRequest request

HttpServletResponse response) throws ServletException, IOException {

Response.setContentType ("text/javascript"); / / pay attention to this

PrintWriter out = response.getWriter ()

String callback = request.getParameter ("callback")

Out.print (callback

+ "([{name:'John',age:'19'}, {name:'joe',age:'20'}])")

System.out.println ("callback =" + callback)

Out.flush ()

Out.close ()

}

2. Key codes of the interface:

$.ajax ({

Url: "http://localhost:8081/JsonpServer/JsonpServlet",

DataType: "jsonp"

Jsonp: "callback", / / passed to the request handler or page to obtain the parameter name of the jsonp callback function name (default: callback), which should be consistent with the server side

JsonpCallback: "person", / / the name of the custom jsonp callback function, which is the random function name automatically generated by jQuery by default

Success:function (json) {

/ / alert (json [0] .name)

}

});

/ / equivalent to the function performed after rewriting success (customized to person)

Function person (json) {

Alert ("I am person function...")

Alert (json [0] .name)

}

Analysis:

At first, it didn't work as dataType: "json", so the test didn't come out for a long time, and then it was changed to dataType: "jsonp", and it can be used.

The reason is that when dataType is in json data format, it cannot be accessed across domains, and jsonp is accessed across domains.

These are all the contents of the article "data Type instance Analysis of Ajax". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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: 285

*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