Home > Backend Development > PHP Tutorial > Detailed explanation of 3 ajax value transfer methods

Detailed explanation of 3 ajax value transfer methods

零到壹度
Release: 2023-03-23 12:38:02
Original
4785 people have browsed it

The content shared with you in this article is a detailed explanation of the three ajax value transfer methods, which has a certain reference value. Friends in need can refer to it

var data=JSON.stringify({‘name”:name,”sex”:sex});
$.ajax({    
//ajax传值方式:1,url后面直接跟参数(get),2,headers(get):传值方式。此处传值的时候,需要设置服务端的Access-Control-Allow-Headers中设置对应的参数名,才
//可以在header中加入想要传值的名字。3,body:传值方式(对应post),也就是data:{中间是json字符串/json对象}
                            url: restApiServer+ 'user?name=' + name,  服务端取值:request.query.name
                            type: 'GET',
                            dataType:"json",
                            headers: {"Authorization": "Basic " + Authorization + ":" +password, "code": code, "startIndex":jtParams.jtStartIndex, "pageSize": jtParams.jtPageSize,"sorting": jtParams.jtSorting }, 服务端取值:request.header(“参数名”);
                            data: data,||data:JsonObject,||  data:JsonStr   服务端取值:request.body.参数名,
                            success: function(data) {
                                if(data.success == 1) {
                                   if(data.data.length < 1)
                                        return;
                                    var jsonObj= {
                                       "Result": "OK",
                                       "Records": data.data,
                                       "TotalRecordCount": data.count["FOUND_ROWS()"]
                                    }
                                   $dfd.resolve(jsonObj);
                                }
                                else {
                                    var jsonObj= {
                                       "Result": "ERROR",
                                       "Message": data.error
                                    }
                                    $dfd.resolve(jsonObj);
                                }
                            },error: function (){   $dfd.reject();  }
                        });
Copy after login

1, use When using the express() framework:

Client:
1) The url is directly followed by the parameter (get) Server-side value: request.query.Parameter name, where name is the parameter name
2 ) headers (get/post), when passing the value here, you need to set the corresponding parameter name in Access-Control-Allow-Headers for cross-domain access. Server-side value: request.header('parameter name');
3) body (Post): that is, the middle data (the middle is a Json string/Json object) Server-side value: request.body. Parameter name

2, use koa2() When using the framework

1) The url is directly followed by the parameter (get) Server-side value: ctx.request.query.Parameter name, where name is the parameter name
2) Headers (get/post), When passing values ​​here, there is no need to use headers to register across domains. Server-side value: ctx.request.header. Parameter name;
3) body (Post): which is the middle data (the middle is a Json string/Json object) Server-side value: ctx.request.body. Parameter name

Related recommendations:

Native ajax value transfer

About the difference between MVC value-passing methods

Detailed explanation of the two parameter-passing methods in Ajax

The above is the detailed content of Detailed explanation of 3 ajax value transfer methods. 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