Home  >  Article  >  Web Front-end  >  How can Jsonp solve ajax cross-domain

How can Jsonp solve ajax cross-domain

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 16:42:421452browse

This time I will show you how Jsonp can solve ajax cross-domain, and what are the precautions for Jsonp to solve ajax cross-domain. The following is a practical case, let's take a look.

1. Introduction

There have been a lot of cross-domain issues recently, and I happened to see this one, so I summarized it, about JSONP There are indeed a lot of things on Baidu, and many people copy others. If this continues, I can actually find only a few pieces of information. The key is that I still can’t understand it. It may be a matter of ability. After many attempts, I have summarized it. After a while, I finally figured it out. One thing to note is that Jsonp is used here to solve the cross-domain problem of ajax. The specific implementation is not actually ajax.

1. Same-Origin Policy

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

2. JSONP

JSONP (JSON with Padding) is a "usage mode" of JSON that can be used to solve the problem of cross-domain data access by mainstream browsers . Due to the same-origin policy, generally speaking, web pages located at server1.example.com cannot communicate with servers other than server1.example.com, with the exception of the HTML script element. Using this open policy of the <script> element, web pages can obtain 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 executed with a JavaScript interpreter instead of parsed with a JSON parser. <p style="text-align: left;"><p style="text-align: left;"><span style="color: #ff0000">2. Practice<strong><p style="text-align: left;">1. Simulate cross-domain requests<strong>Make two on this machine There is a tomcat, and the ports are 8080 and 8888 respectively, which meets the condition of non-same origin. If you send ajax from one port to obtain data from another port, a cross-domain request problem will definitely be reported. <p style="text-align: left;"><p style="text-align: left;"><img title="" alt="How can Jsonp solve ajax cross-domain" src="https://img.php.cn/upload/article/000/061/021/43aef952ef7b1b5d0f454cdb6aaedab2-0.png"/>There are two projects here, namely jsonp (8080) and other (8888). The index.jsp in the jsonp project is as follows: <p style="max-width:90%"><pre class="brush:php;toolbar:false"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Insert title here <script type="text/javascript" src="js/jquery.min.js"></script> <script> function jsonp_fun(){ $.ajax({ url:&#39;http://localhost:8888/other/index.jsp&#39;, type:&#39;post&#39;, dataType:&#39;text&#39;, success:function(data){ console.log(data); } }); } </script>

The above is the detailed content of How can Jsonp solve ajax cross-domain. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn