


How to let vue's axios component and PHP backend exchange data
This article mainly introduces how to exchange data between Vue's axios component and PHP backend. It has certain reference value. Now I share it with you. Friends in need can refer to it
1. Preface
axios is a component in the vue project that uses ajax technology to exchange data with the background. Under the recommendation of the author of vue, a considerable number of vue front-end developers began to use it. However, during the actual development process, it sometimes happens that the backend cannot receive the data posted by the frontend. Taking the background of PHP language development as an example, PHP's native predefined variable $_POST cannot be received (because parsing failed). The purpose of this article is to explore front-end and back-end data interaction and provide different solutions for your reference.
2. Currently, the data form that $_POST can receive
Form Data
This data form is actually a key-value pair, such as id:1, if there is If there are multiple sets of key-value pairs and they are nested, then the following is the case
1 2 3 4 5 6 7 8 9 |
|
The data received by the PHP server actually looks like this
1 |
|
Is it particularly similar to the parameters of the url?
The data of this key-value pair is called QueryString. When the browser's native form sends this data, the request header will be set to application/x-www-form-urlencoded .
QueryString can be successfully parsed by PHP's $_POST
The serialize() method and param() method of jQuery ajax under the classic front-end library jQuery is to convert data into The solution provided by QueryString is that the former converts form data and the latter converts JSON data.
Moreover, jQuery's ajax request will determine the incoming data form and implicitly call the param() method to convert the json data. Therefore, the user only needs to directly provide the json data to successfully submit the data to the background. It needs to be explicitly There are not many opportunities to call the param() method (manually). The request header sent by jq by default is also application/x-www-form-urlencoded. In most cases, the user does not need to set it manually.
Back to our axios, the request header sent by axios by default is application/json. To put it simply, it will pass json to the backend by default and not convert it into QueryString. .
3. Solution
1. The front-end converts the data into QueryString
Introduce the qs library and call the stringify method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
2. Use PHP back-end php://input gets the original data
Without any changes to the front-end, because the predefined variable $_POST cannot be parsed, the php back-end can only use php://input to get the original data, and then Then convert it into the desired data form.
If the PHP backend uses native development, you can customize a function to process the data of each request.
If the PHP backend is developed using a specific framework, it depends on whether the framework itself supports processing php://input. A simple test method is to search for php://input in the full text of the source code of the framework. If it can be found, If it is supported, otherwise it is not supported and you need to extend the relevant processing code yourself.
Taking the PHP framework yii2.0 as an example, search the vendor directory in full text, and you can see that there is relevant processing code in line 494 of yiisoft->yii2->web->Request.php.
In actual use, you only need to configure the configuration file web.php
1 2 3 4 5 6 7 8 9 |
|
The above are two solutions for front-end processing and back-end processing, you can choose according to the actual situation
The above is the entire article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
How to set up a long connection for the yii database
The above is the detailed content of How to let vue's axios component and PHP backend exchange data. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Vue.js is suitable for small and medium-sized projects and fast iterations, while React is suitable for large and complex applications. 1) Vue.js is easy to use and is suitable for situations where the team is insufficient or the project scale is small. 2) React has a richer ecosystem and is suitable for projects with high performance and complex functional needs.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Vue.js is not difficult to learn, especially for developers with a JavaScript foundation. 1) Its progressive design and responsive system simplify the development process. 2) Component-based development makes code management more efficient. 3) The usage examples show basic and advanced usage. 4) Common errors can be debugged through VueDevtools. 5) Performance optimization and best practices, such as using v-if/v-show and key attributes, can improve application efficiency.

Vue.js is mainly used for front-end development. 1) It is a lightweight and flexible JavaScript framework focused on building user interfaces and single-page applications. 2) The core of Vue.js is its responsive data system, and the view is automatically updated when the data changes. 3) It supports component development, and the UI can be split into independent and reusable components.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
