Home > Web Front-end > JS Tutorial > Detailed explanation of Ajax using jsonp method to obtain data across domains (with code)

Detailed explanation of Ajax using jsonp method to obtain data across domains (with code)

php中世界最好的语言
Release: 2018-04-04 14:37:17
Original
1305 people have browsed it

This time I will bring you a detailed explanation of how Ajax uses jsonp to obtain data across domains (with code). What are the precautions for Ajax using jsonp to obtain data across domains. The following is a practical case. Let’s take a look. take a look.

I came across the jsonp call today and just sorted it out.

<!DOCTYPE html>
<html>
<head>
	<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
 <script type="text/javascript">
	$(document).ready(function(){
	 $("#b01").click(function(){
	 	$.ajax({//danielinbiti.txt文件内容:getAInfo(["<input type=&#39;text&#39; value=&#39;1222&#39;/>"])
    url: 'http://192.168.12.21:8080/systemr/danielinbiti.txt',
    dataType: 'jsonp',//跨域设置jsonp
    processData: false,
    jsonpCallback:'getAInfo',//与文件中的getAInfo对应
    type: 'get',
    success: function(data) {
      $("#myp").html(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
      alert('error');
      alert(XMLHttpRequest.status);
      alert(XMLHttpRequest.readyState);
      alert(textStatus);
    }
   });
	 });
	 $("#b02").click(function(){// 需要在后台根据request获取callback,然后才callback(需要返回的json内容)的方式返回,getJSON是ajax的简化,只支持json格式
	 	 $.getJSON("http://192.168.12.21:8080/systemr/b.do?jsoncallback=?",function(result){
		   $("#myp").html(result);
		  });
	 });
	});
</script>
</head>
<body>
<p id="myp"><h2>通过 AJAX 改变文本</h2></p>
<button id="b01" type="button">改变内容01</button>
<button id="b02" type="button">改变内容02getJSON</button>
</body>
</html>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Jump to the login page immediately after Ajax+Session fails

##Global monitoring of ajax operation, user session How to deal with failure

The above is the detailed content of Detailed explanation of Ajax using jsonp method to obtain data across domains (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template