Javascript-jquery's $. ajax () post data submitted to php is incorrect-php Tutorial

Source: Internet
Author: User
{Code...} is like this. my goal is to perform md5 calculation on the password before submission to encrypt the transmission password. However, the actual result is that when my password is 123, alert ($ (& #039; # password & #039 ;). val () printed out is correct 202cb962ac59075b964b0715...
$ ('# Form1'). submit (function () {$. ajax ({url: 'login. php? Act = login ', data: $ (' # form1 '). serialize (), type: 'post', Ype: 'text', beforeSend: function () {} if ($ ('# password '). val () = '') {alert ('secret cannot be blank '); return false;} else {var p = $ (' # password '). val (); alert (p); $ ('# password '). val (hex_md5 (p); alert ($ ('# password '). val () ;}, success: function (msg ){......};}}); return false ;});

In this case, my goal is to perform md5 calculation on the password before submission to encrypt the transmission password.

However, the actual effect is that when my password is 123, alert ($ ('# password '). val () is printed out to 202cb962ac59075b964b07152d234b70, but after post to the background php, it is still 123.

I feel like data: $ ('# form1'). serialize () has been set before beforeSend, so it is still old.

After testing, if I put the md5 encrypted part out and put it before $. ajax (), there is no problem. It is inconvenient to use it. is there any way to run serialize () after beforSend?

Supplement:
After using the method provided by @ lisfan, the submission is normal.

$ ('# Form1'). submit (function () {$. ajax ({url: 'login. php? Act = login ', ** data: null, ** type: 'post', dataType: 'text', beforeSend: function () {} if ($ ('# password '). val () = '') {alert ('secret cannot be blank '); return false;} else {var p = $ (' # password '). val (); alert (p); $ ('# password '). val (hex_md5 (p); alert ($ ('# password '). val (); ** this. data = $ ('# form1 '). serialize (); **}, success: function (msg ){......};}}); return false ;});

However, no data is received for $ _ REQUEST and $ _ POST in the background. For example:

The following is the post data seen by firebug. above is the data of print $ _ REQUEST, $ _ POST, and php: // input respectively. you can see the data in file_get_contents ("php: // input ") is the raw data, but why is it not encapsulated in $ _ POST?

.

Reply content:
$ ('# Form1'). submit (function () {$. ajax ({url: 'login. php? Act = login ', data: $ (' # form1 '). serialize (), type: 'post', Ype: 'text', beforeSend: function () {} if ($ ('# password '). val () = '') {alert ('secret cannot be blank '); return false;} else {var p = $ (' # password '). val (); alert (p); $ ('# password '). val (hex_md5 (p); alert ($ ('# password '). val () ;}, success: function (msg ){......};}}); return false ;});

In this case, my goal is to perform md5 calculation on the password before submission to encrypt the transmission password.

However, the actual effect is that when my password is 123, alert ($ ('# password '). val () is printed out to 202cb962ac59075b964b07152d234b70, but after post to the background php, it is still 123.

I feel like data: $ ('# form1'). serialize () has been set before beforeSend, so it is still old.

After testing, if I put the md5 encrypted part out and put it before $. ajax (), there is no problem. It is inconvenient to use it. is there any way to run serialize () after beforSend?

Supplement:
After using the method provided by @ lisfan, the submission is normal.

$ ('# Form1'). submit (function () {$. ajax ({url: 'login. php? Act = login ', ** data: null, ** type: 'post', dataType: 'text', beforeSend: function () {} if ($ ('# password '). val () = '') {alert ('secret cannot be blank '); return false;} else {var p = $ (' # password '). val (); alert (p); $ ('# password '). val (hex_md5 (p); alert ($ ('# password '). val (); ** this. data = $ ('# form1 '). serialize (); **}, success: function (msg ){......};}}); return false ;});

However, no data is received for $ _ REQUEST and $ _ POST in the background. For example:

The following is the post data seen by firebug. above is the data of print $ _ REQUEST, $ _ POST, and php: // input respectively. you can see the data in file_get_contents ("php: // input ") is the raw data, but why is it not encapsulated in $ _ POST?

.

Without modifying the original structure of the subject, try to modify the following:*Asterisk position

$ ('# Form1'). submit (function () {$. ajax ({url: 'login. php? Act = login ', ** data: null, ** type: 'post', dataType: 'text', beforeSend: function () {} if ($ ('# password '). val () = '') {alert ('secret cannot be blank '); return false;} else {var p = $ (' # password '). val (); alert (p); $ ('# password '). val (hex_md5 (p); alert ($ ('# password '). val (); ** this. data = $ ('# form1 '). serialize (); **}, success: function (msg ){......};}}); return false ;});

You can process the password and send it again $. the AJAX () request is to process the content of the beforeSend function before sending the request. in this way, $ ('# form1 '). the data of serialize () is correct.

This indicates that $ ('# form1'). serialize () is called before beforeSend.

We recommend that you encrypt the password and assign it to a hidden field in the form if the verification function passes the verification.

$ ('# Form1 '). submit (function () {if ($ ('# password '). val () = '') {alert ('secret cannot be blank '); return false;} var p = $ (' # password '). val (); alert (p); $ ('# password '). val (hex_md5 (p); alert ($ ('# password '). val (); $. post ('login. php? Act = login ', $ (this). serialize (), function (msg) {console. log (msg) ;}); return false ;});

Take out the judgment and put ajax behind the password encryption.

This is because $. in ajax, the js engine creates a literal object. When an object is created, the data value is saved as $ ('# form1') at the time '). the returned value of serialize () is 123.
See the following example:
Var c = 1;
Var a = {

b:c,d : function(){    c=2}

};
Console. log (a. B); // 1
A. d (); // c = 2
Console. log (a. B); // 1
This explains the problem above.
The possible solution is to only move md5 encryption before creating a literal object.

First, since md5 is required, why should it be assigned to the input of the password? is there no security risk? Second, the beforeSend function is called before the request is sent, instead of before the Ajax method. when you call the ajax method, you have removed the form data and set it to the function parameters, then how can the modified form values be automatically updated to ajax data? it's just a whimsical idea. I mentioned the md5 password operation before calling the Ajax method.

Set the encrypted string before the ajax method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.