Encapsulation examples of ajax network requests
The following editor will bring you an example of encapsulation of ajax network requests. The editor thinks it's pretty good, so now I'll share the ajax source code with you and give it as a reference. If you are interested in ajax, please follow the editor to take a look
Example code:
// 封装的ajax网络请求函数 // obj 是一个对象 function AJAX(obj){ //跨域请求 if (obj.dataType == "jsonp") { //在这里 callback 必须是全局变量 保证函数消失的时候 这个变量不可以被销毁 //处理一下函数名(防止多个网络请求 函数名字相同 出现紊乱的情况) var hehe = "callBack" + "_" + new Date().getTime() + "_" + String(Math.random()).replace(".",""); window[hehe] = obj.success; //创建 script标签 var sc = document.createElement("script"); sc.src = obj.url + "?" + "cb=" + hehe; console.log(sc.src); document.body.appendChild(sc); document.body.removeChild(sc); return; } //1、创建 ajax 对象 var ajaxObj = null; if (window.XMLHttpRequest) { ajaxObj = new XMLHttpRequest(); }else{ ajaxObj = new ActiveXObject("Microsoft.XMLHTTP"); } //设置请求的类型 obj.type = obj.type.toUpperCase() || "GET"; //如果是get请求 并且需要传递参数 则需要给 url 后面拼接参数 if (obj.type == "GET") { var arr = [];//定义数组 用于把对象存储到数据里面 for (var key in obj.data) { arr.push(key +"="+ obj.data[key]); } //用&分隔数组 让其转化为类似:name=lxl&age=18 的形式 var str = arr.join("&"); obj.url = obj.url +"?"+ str; //拨号 ajaxObj.open(obj.type,obj.url,true); //发送"name=123&age=18" ajaxObj.send(); }else{ var arr = [];//定义数组 用于把对象存储到数据里面 for (var key in obj.data) { arr.push(key +"="+ obj.data[key]); //console.log(arr); } //用&分隔数组 让其转化为类似:name=lxl&age=18 的形式 var str = arr.join("&"); //console.log(str); ajaxObj.open(obj.type,obj.url,true); ajaxObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); ajaxObj.send(str); } //监听 ajaxObj.onreadystatechange = function(){ if (ajaxObj.readyState == 4) { if (ajaxObj.status >= 200 && ajaxObj.status < 300 || ajaxObj.status == 304) { //请求成功 obj.success(ajaxObj.responseText); }else{ //请求失败 obj.error(ajaxObj.status); } } } }
The above encapsulation example of ajax network request is all the content shared by the editor. I hope it can give you a reference, and I also hope that everyone will support the PHP Chinese website.
Related recommendations:
How to solve the problem of arrays in AJAX requests
Detailed explanation of several ajax request methods that may be encountered in actual combat
Ajax request and Filter cooperation case details
The above is the detailed content of Encapsulation examples of ajax network requests. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

According to news from this site on April 17, TrendForce recently released a report, believing that demand for Nvidia's new Blackwell platform products is bullish, and is expected to drive TSMC's total CoWoS packaging production capacity to increase by more than 150% in 2024. NVIDIA Blackwell's new platform products include B-series GPUs and GB200 accelerator cards integrating NVIDIA's own GraceArm CPU. TrendForce confirms that the supply chain is currently very optimistic about GB200. It is estimated that shipments in 2025 are expected to exceed one million units, accounting for 40-50% of Nvidia's high-end GPUs. Nvidia plans to deliver products such as GB200 and B100 in the second half of the year, but upstream wafer packaging must further adopt more complex products.

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:

This website reported on July 9 that the AMD Zen5 architecture "Strix" series processors will have two packaging solutions. The smaller StrixPoint will use the FP8 package, while the StrixHalo will use the FP11 package. Source: videocardz source @Olrak29_ The latest revelation is that StrixHalo’s FP11 package size is 37.5mm*45mm (1687 square millimeters), which is the same as the LGA-1700 package size of Intel’s AlderLake and RaptorLake CPUs. AMD’s latest Phoenix APU uses an FP8 packaging solution with a size of 25*40mm, which means that StrixHalo’s F

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

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.

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.
