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

How to solve the Cross-domain problem of ajax by Jsonp

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

Share

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

This article mainly shows you "Jsonp how to solve ajax cross-domain problems", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Jsonp how to solve ajax cross-domain problems" this article.

I. introduction

Recently, there are many cross-domain problems, and I happen to see this piece, so I summed up that there are indeed a lot of things about JSONP Baidu, and many people copy other people's. If it goes on like this, I will find only a few copies of information. The key is that I still can't understand it. Maybe it's a problem of ability. After a lot of attempts, I summed it up and finally got to the surface. Note that Jsonp is used to solve the cross-domain problem of ajax, and the specific implementation is not ajax.

1. Homologous strategy

Browsers have a very important concept-homologous policy (Same-Origin Policy). The so-called homology means that the domain name, protocol and port are the same. Client scripts from different sources (JavaScript, ActionScript) cannot read or write to each other's resources without explicit authorization.

2 、 JSONP

JSONP (JSON with Padding) is a "usage mode" of JSON, which can be used to solve the problem of cross-domain data access in mainstream browsers. Due to the same origin policy, Web pages located in server1.example.com generally cannot communicate with servers that are not server1.example.com, and the script element of HTML is an exception. Using this open strategy of elements, web pages can get JSON data dynamically generated from other sources, and this usage pattern is called JSONP. The data captured with JSONP is not JSON, but arbitrary JavaScript, which is parsed with the JavaScript literal translator rather than the JSON parser.

II. Practice

1. Simulate cross-domain requests

Get two tomcat on the local machine, and the port is 8080808888, which meets the condition of non-homology, so if you send ajax from one port to get the data of the other port, you will definitely report a cross-domain request problem.

There are two projects, jsonp (8080) and other (8888). The index.jsp in the jsonp project is as follows:

Insert title herefunction jsonp_fun () {$.ajax ({url:' http://localhost:8888/other/index.jsp',type:'post',dataType:'text',success:function(data){console.log(data);}});}

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