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

Source: Internet
Author: User
Tags php print
$('#form1').submit(function(){                $.ajax({             url:'login.php?act=login',            data:$('#form1').serialize(),            type:'POST',            dataType:'text',            beforeSend:function(){                }                if($('#password').val()==''){                    alert('密碼不能為空');                    return false;                }else{                    var p = $('#password').val();                    alert(p);                    $('#password').val(hex_md5(p));                    alert($('#password').val());                }            },            success:function(msg){                ......                };            }        });        return false;    });

So, my aim is to md5 the password before committing it to encrypt the transfer password.

But the practical effect is that when I password a value of 123, alert ($ (' #password '). Val ()) prints out the right 202cb962ac59075b964b07152d234b70, but after post to PHP in the background, It's still 123.

I feel like a data:$ (' #form1 '). Serialize () has been evaluated before beforesend, so it is passed to the back or old value.

After testing, if I took the MD5 encrypted piece out and put it in the $.ajax () before it was done, there was no problem. It is not convenient to use this, there is no way to let Serialize () in beforsend after the implementation of it?

Add:
After using the method provided after pasting @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('密碼不能為空');                    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, the background PHP $_request and $_post are not receiving data. Such as:

Here is the post data that Firebug sees, which is the data for PHP print $_request, $_post, and Php://input, which can be seen in file_get_contents ("Php://input") The original data is seen, but why is it not encapsulated in the $_post?

.

Reply content:

$('#form1').submit(function(){                $.ajax({             url:'login.php?act=login',            data:$('#form1').serialize(),            type:'POST',            dataType:'text',            beforeSend:function(){                }                if($('#password').val()==''){                    alert('密碼不能為空');                    return false;                }else{                    var p = $('#password').val();                    alert(p);                    $('#password').val(hex_md5(p));                    alert($('#password').val());                }            },            success:function(msg){                ......                };            }        });        return false;    });

So, my aim is to md5 the password before committing it to encrypt the transfer password.

But the practical effect is that when I password a value of 123, alert ($ (' #password '). Val ()) prints out the right 202cb962ac59075b964b07152d234b70, but after post to PHP in the background, It's still 123.

I feel like a data:$ (' #form1 '). Serialize () has been evaluated before beforesend, so it is passed to the back or old value.

After testing, if I took the MD5 encrypted piece out and put it in the $.ajax () before it was done, there was no problem. It is not convenient to use this, there is no way to let Serialize () in beforsend after the implementation of it?

Add:
After using the method provided after pasting @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('密碼不能為空');                    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, the background PHP $_request and $_post are not receiving data. Such as:

Here is the post data that Firebug sees, which is the data for PHP print $_request, $_post, and Php://input, which can be seen in file_get_contents ("Php://input") The original data is seen, but why is it not encapsulated in the $_post?

.

Under the premise of not modifying the main original structure, try to modify the following to see * the position with asterisks

$('#form1').submit(function(){                $.ajax({             url:'login.php?act=login',            **data:null,**            type:'POST',            dataType:'text',            beforeSend:function(){                }                if($('#password').val()==''){                    alert('密碼不能為空');                    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 before sending $. The AJAX () request is to process the contents of the Beforesend function before sending the request, so that $ (' #form1 '). Serialize () data is correct

This indicates $ (' #form1 '). Serialize () This function was called before beforesend.

It is recommended that after you have entered the password, the validation function will encrypt it and assign it to a hidden field in the form if it is passed.

$('#form1').submit(function() {  if ($('#password').val() == '') {    alert('密碼不能為空');    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 the judgment out, Ajax put it behind the password encryption.

This is because when the $.ajax is executed, the JS engine creates a literal object, and when the object is created, the value of data is stored for $ (' #form1 ') at that time. Serialize () The return value, which is 123.
You can look at 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 can explain the above problem.
A possible workaround is to move MD5 encryption before creating a literal object.

First of all, since to MD5, why also to assign value to password input, there is no security risk? Second, the Beforesend function is called before the request is sent, not before the Ajax method, when you call the Ajax method, the form data is removed, set to the parameters of the function, then the modified form value can be automatically updated to the AJAX data, It's just a fantasy at all. The operation of the password MD5 is mentioned before the Ajax method call.

A string that encrypts the password before the Ajax method is done

  • Related Article

    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.