Hongmu Notes
Home Language Notes jQuery click Ajax to submit data
Language Notes JavaScript

jQuery click Ajax to submit data

jQuery click Ajax to submit data

The following is a simple click event that uses jQuery to send an AJAX request and submit the data to the server, and display a pop-up window when the data is returned:

$(document).ready(function() {  
  $('button').click(function() {  
    // 定义要提交的数据  
    var data = {  
      name: 'John',  
      age: 30  
    };

    // 发送 AJAX 请求  
    $.ajax({  
      url: 'https://example.com/api/submit',  
      type: 'POST',  
      data: data,  
      success: function(response) {  
        // 弹窗显示返回数据  
        alert(response);  
      },  
      error: function() {  
        // 如果发生错误,弹出错误提示  
        alert('Error');  
      }  
    });  
  });  
});

In this example, we first bind to a button click event after the document is ready. When the button is clicked, we use.ajax()The method sends a POST request to the specified URL, sending the defined data as the request body. On a successful response we use.success()The method handles the pop-up window display and pops up an alert box containing the returned data. If an error occurs we use.error()Method to handle error prompts.

Please note that this code example is for demonstration purposes only and may need to be modified based on actual needs.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

js move the mouse up to modify the style attribute

js move the mouse up to modify the style attribute Language Notes JavaScript

When the mouse hovers over the element, the color changes to red; when the mouse leaves the element, the color changes to black. To retrieve an element by its ID: `document.getElementById()`; Event handler for mouse hover: `onmouseover`; Event handler for mouse leave: `onmouseout`.<!DOCTYPE html><html lang= "en">...
👁 130

Recommended reading

(Adaptive mobile version) Green Hardware Parts – English pbootCMS Foreign Trade Website Template; Download source code for websites specializing in precision mold part manufacturing – 0389

(Adaptive mobile version) Green Hardware Parts – English pbootCMS Foreign Trade Website Template; Download source code for websites specializing in precision mold part manufacturing – 0389 Practical Collection pbootcms Template

This adaptive mobile-friendly English-language PbootCMS export-oriented website template for green hardware components is designed with a professional international style, ideal for precision mold component manufacturing enterprises to showcase their export products. It helps Chinese hardware manufacturing companies demonstrate their technical expertise and product quality in overseas markets. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn – Related...
👁 65
Outdoor ski training equipment website template 0695

Outdoor ski training equipment website template 0695 Practical Collection Yiyou template

This EyouCMS template is designed for the outdoor skiing training and equipment industry. Its vibrant winter-themed design effectively showcases skiing training courses, skiing equipment, coaching teams, and skiing tourism services, helping skiing training institutions attract skiing enthusiasts online. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (...
👁 32
PHP serialize() function

PHP serialize() function Language Notes PHP

The `serialize()` function serializes an object or an array and returns a string. After serializing an object, you can easily pass it to other parts of your code that require it, without any changes to its type or structure. To convert a serialized string back into a PHP value, you can use the ` unserialize()` function. PHP version requirement: PHP 4, PH...
👁 148
(PC+WAP) Air Conditioning Installation & Maintenance Website Template; Home Appliance Repair Website Source Code Download – 1117

(PC+WAP) Air Conditioning Installation & Maintenance Website Template; Home Appliance Repair Website Source Code Download – 1117 Practical Collection pbootcms Template

A PbootCMS website template for air conditioning installation/repair and household appliance repair services, compatible with both PC and WAP devices. The professional and practical design is ideal for air conditioning installation and repair service providers to showcase their business offerings. It helps appliance service companies present their services online and attract local customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 50
(Adaptive mobile version) PBootCMS template for website development – Download responsive, high-end corporate website source code – 0390

(Adaptive mobile version) PBootCMS template for website development – Download responsive, high-end corporate website source code – 0390 Practical Collection pbootcms Template

An adaptive mobile-friendly website development and web development template from PbootCMS – designed with a modern, tech-driven aesthetic, ideal for web development agencies and IT service providers to showcase their portfolio, services, and technical expertise. It helps web development companies attract business clients looking for website development solutions online. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 38