In-depth analysis of Ajax and cross-domain issues_AJAX related
This article mainly introduces Ajax and cross-domain issues in detail, telling you what Ajax is and what is cross-domain? It has certain reference value. Interested friends can refer to it
What is ajax
XMLHttpRequest object
Usage of XHR
1. Create an XMLHttpRequest object2. Send a request1).Set the request line xhr.open( )2).
POST requestNeeds to set the request header xhr.setRequestHeader() The value of POST request header Content-Type: application/x-www-form-urlencoded3).Set request Body xhr.send() get request passes null, post depends on the situation
status codeand the asynchronous object are parsed Completed.
1xx:Message2xx:Success
3xx:Redirect
4xx:Request error
5xx: Server error
1: The asynchronous object initialization is completed and the request is sent
2: Receive the original data returned by the server
3: The data is being parsed, and parsing takes time
4: The data parsing is completed, and the data can be used
XML
Characteristics of XML, from a famous family, developed by W3C, a data format that has been strongly recommended by Microsoft and IBM. It is designed to represent pages. Grammar rules: Similar to HTML, they are expressed through tags1. When the backend returns, set the Content-Type value in the response header to application/xml
2. The frontend is asynchronous When the object receives background data, remember to receive it in the form of xml, xhr.responseXML, and it returns an
object object with the content of #document
JSON
JSON (JavaScript Object Notation) comes from the grassroots. It is a subset of Javascript and is responsible for describing the data format. JSON itself is a string in a special format that can be converted into a js object and is a network There is no one of the most widely used data formats for transmitting data. Grammar rules: Data is represented by key/value pairs, and the data is separated by commas , curly brackets save objects, square brackets save arrays, names and values need to be enclosed in double quotes (this is a small difference from js).
Parsing/manipulating JSON in js: 1.JSON.parse(json string); Parse a json format string into a js object
2.JSON.stringify(js object); Convert a js object into a json format The string
function pinjieData(obj) { //obj 就相当于 {key:value,key:value} //最终拼接成键值对的字符串 "key:value,key:value" var finalData = ""; for(key in obj){ finalData+=key+"="+obj[key]+"&"; //key:value,key:value& } return finalData.slice(0,-1);//key:value,key:value } function ajax(obj) { var url = obj.url; var method = obj.method.toLowerCase(); var success = obj.success; var finalData = pinjieData(obj.data); //finalData最终的效果key:value,key:value //1.创建xhr对象 var xhr = new XMLHttpRequest(); //get方法拼接地址,xhr.send(null) if (method=='get'){ url = url + "?"+finalData; finalData = null; } //2.设置请求行 xhr.open(method,url); // 如果是post请求,要设置请求头 if (method=='post'){ xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded") } //3.发送 xhr.send(finalData); //4.监听服务器返回的数据 xhr.onreadystatechange = function () { if (xhr.status==200 && xhr.readyState==4){ var result = null; //获取返回的数据类型 var rType = xhr.getResponseHeader("Content-Type"); if (rType.indexOf('xml')!=-1){ result = xhr.responseXML; }else if(rType.indexOf('json')!=-1){ // JSON.parse 的意思是 将 json格式的字符串 //比如 [{ "src":"./images/nav0.png","content":"商品分类1"}] //转成js对象 result = JSON.parse(xhr.responseText); }else{//当成普通的字符串去处理 result = xhr.responseText; } //将这里解析好的数据交给页面去渲染 success(result); } } }
Use ajax## in jQuery #API jQuery ajaxjQuery provides us with a more convenient ajax package.
$.ajax({}) can be configured to initiate an ajax request
$.get() to Initiate ajax request in get mode$.post() Initiate ajax request in post mode
$('form').serialize() Serialize form (format key=val$key=val)
url:Interface address
type:Request method (get/post)timeout:Required to be Number type parameter, set request timeout Time (milliseconds)
dataType: It should be a value passed by the client to the server, telling the server how to process:
data: Send request data
beforeSend: It is required to be a parameter of Function type, which can be modified before sending the request Functions of the XMLHttpRequest object, such as adding custom HTTP headers. If false is returned in beforeSend, this ajax request can be canceled.
success: Called after a successful response
error: Called when an error response
complete: Called when the response is completed (including success and failure)
//ajax===get $.ajax({ url:'', data:'key=value&key=value', type:'get', success:function (result) { console.log(result); } }); //ajax===post $.ajax({ url:'', data:'key=value&key=value', type:'post', success:function (result) { console.log(result); } }); //$.get $.get({ url:'', data:'key=value&key=value', success:function (result) { console.log(result); } }); //$.post $.post({ url:'', data:'key=value&key=value', success:function (result) { console.log(result); } }); //在使用jQuery中ajax发送请求的时候,只需要在 dataType中写上jsonp即可实现ajax的跨域请求 dataType:'jsonp'
A major limitation of ajax communication via XHR (same domain , same port, same protocol), derived from cross-server security policy. By default, XHR can only request resources in the same domain. This is to prevent certain malicious behaviors.
CORS cross domain CORS(cross-origin resource sharing,跨域源资源共享)定义了在跨域时,浏览器和服务器应该如何沟通.CORS允许一个域上的网络应用向另一个域提交跨域 AJAX 请求。实现此功能非常简单,只需由服务器发送一个响应标头即可。 JSONP JSONP由回调函数和数据组成.JSONP只支持GET请求.JSONP的优势在于支持老式浏览器,以及可以向不支持CORS的网站请求数据. AI-powered app for creating realistic nude photos Online AI tool for removing clothes from photos. Undress images for free AI clothes remover Swap faces in any video effortlessly with our completely free AI face swap tool! Easy-to-use and free code editor Chinese version, very easy to use Powerful PHP integrated development environment Visual web development tools God-level code editing software (SublimeText3)
CORS支持所有类型的HTTP请求.
服务器端对于CORS的支持,主要就是通过设置Access-Control-Allow-Origin来进行的。
JSONP通过动态
Hot AI Tools
Undresser.AI Undress
AI Clothes Remover
Undress AI Tool
Clothoff.io
Video Face Swap
Hot Article
Hot Tools
Notepad++7.3.1
SublimeText3 Chinese version
Zend Studio 13.0.1
Dreamweaver CS6
SublimeText3 Mac version
Hot Topics
1389
52

Detailed explanation of Oracle error 3114: How to solve it quickly, specific code examples are needed. During the development and management of Oracle database, we often encounter various errors, among which error 3114 is a relatively common problem. Error 3114 usually indicates a problem with the database connection, which may be caused by network failure, database service stop, or incorrect connection string settings. This article will explain in detail the cause of error 3114 and how to quickly solve this problem, and attach the specific code

Wormhole is a leader in blockchain interoperability, focused on creating resilient, future-proof decentralized systems that prioritize ownership, control, and permissionless innovation. The foundation of this vision is a commitment to technical expertise, ethical principles, and community alignment to redefine the interoperability landscape with simplicity, clarity, and a broad suite of multi-chain solutions. With the rise of zero-knowledge proofs, scaling solutions, and feature-rich token standards, blockchains are becoming more powerful and interoperability is becoming increasingly important. In this innovative application environment, novel governance systems and practical capabilities bring unprecedented opportunities to assets across the network. Protocol builders are now grappling with how to operate in this emerging multi-chain

[Analysis of the meaning and usage of midpoint in PHP] In PHP, midpoint (.) is a commonly used operator used to connect two strings or properties or methods of objects. In this article, we’ll take a deep dive into the meaning and usage of midpoints in PHP, illustrating them with concrete code examples. 1. Connect string midpoint operator. The most common usage in PHP is to connect two strings. By placing . between two strings, you can splice them together to form a new string. $string1=&qu

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

Analysis of new features of Win11: How to skip logging in to a Microsoft account. With the release of Windows 11, many users have found that it brings more convenience and new features. However, some users may not like having their system tied to a Microsoft account and wish to skip this step. This article will introduce some methods to help users skip logging in to a Microsoft account in Windows 11 and achieve a more private and autonomous experience. First, let’s understand why some users are reluctant to log in to their Microsoft account. On the one hand, some users worry that they

Due to space limitations, the following is a brief article: Apache2 is a commonly used web server software, and PHP is a widely used server-side scripting language. In the process of building a website, sometimes you encounter the problem that Apache2 cannot correctly parse the PHP file, causing the PHP code to fail to execute. This problem is usually caused by Apache2 not configuring the PHP module correctly, or the PHP module being incompatible with the version of Apache2. There are generally two ways to solve this problem, one is
