2016-05-14 05:01
浏览 249

如何正确传递jquery .ajax()函数的数据字符串参数?

var username = $('#username').val();

var dataString = 'username=' + username;

        type: "POST",
        url: "signinout.php",
        data: dataString,
        success: function() {
            $('.user').html('<span>Welcome <span id="loggedUser">' + username + '</span>!</span> <a id="signOut" onclick="window.location.reload()">SIGN OUT</a>');

using the above code, my username variable is not being passed on correctly, I'm assuming something is wrong with the way I coding the datastring parameter but I'm not sure how to do it correctly.

Below is the php code that I am using in signinout.php to insert the username into the database, the username field is not being entered with each new entry into the database.

$username = protect($_POST['username']);
$time = time();

$sql = "INSERT INTO users
    (username, join_date)
    ('$username', '$time')";
$result = mysqli_query($cn, $sql) or

图片转代码服务由CSDN问答提供 功能建议

  var username = $('#username')。val(); 
var dataString =  'username ='+ username; 
 $ .ajax({
  $('。user')。html('&lt; span&gt; Welcome&lt; span id =“loggedUser”&gt;'+ username +'&lt; / span&gt;!&lt; / span&gt;&lt; a id =“signOut”  onclick =“window.location.reload()”&gt; SIGN OUT&lt; / a&gt;'); 

使用上述内容 代码,我的用户名变量没有正确传递,我假设编码datastring参数的方式有问题,但我不知道如何正确地执行。


   $ username = protect($ _ POST ['username']); 
 $ time = time(); 
 $ sql =“INSERT INTO users 
('$ username','$ time')“; 
 $ result = mysqli_query($ cn,$ sql)或
 die(mysqli_error($ cn)); 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanliu8998 2016-05-14 05:26

    Your "best" datastring depends on your needs in the server side part. As an example, this jquery-ajax call send a object to a server side action (PHP) :

    var mydata = null;
    mydata = "hellostring=1";
    mydata = { title: "some" , value: "thing" };
    mydata = [1,2,3];
       cache: false, type: 'post', async: true, 
       data: mydata,
       url: 'some-script.php',
       success: function(resp){
       error: function(e){

    As result, in your serve side you may have this script, which will return the same as you send:

    // some-script.php
        echo print_r($_POST,true);

    The outputs, for each kind of data (see the mydata variable) is:

    Case: mydata = "hellostring=1";

        Array( [hellostring] => "1" )    

    this mean, in serverside, you can:

     $_123 = $_POST["hellostring"];  

    Case mydata = { title: "some" , value: "thing" };

    As result, you get:

        [title] => some
        [value] => thing

    So you can:

    $title = $_POST['title'];  $value = $_POST['value'];

    Case mydata = [1,2,3];

    Surprise, this doesnt work, :) , you should wrap it, in this form:

    mydata = { some : [1,2,3] }

    So, you can proceed in your server-side the same as the previous case.


    To avoid get hacked: (PHP CASE example) filter your input using:


    In order to have a more advanced data handling in your server side part, (that is: in the script who receive the ajax request) , you can make usage of json, in this way:

    Let start by supposing you are sending a object via javascript:

    // in your client part, 
       mydata = { title: "some" , value: "thing", mydog: "sammy" }; your ajax call stuff here..

    And, in your server side:

           // some-script.php
           $obj = json_decode(file_get_contents('php://input'));
           echo $obj->title;  // output: "some"
           echo $obj->value;  // output: "thing"
           echo $obj->mydog;  // output: "sammy"
    打赏 评论
  • donglingyi4679 2016-05-14 05:03

    try passing it as a regular javascript object

    var dataObj = {'username': username};
        type: "POST",
        url: "signinout.php",
        data: dataObj,
    打赏 评论
  • doufan3958 2016-05-14 05:07

    Try using data: "username="+username, instead

    var username = $('#username').val();
        type: "POST",
        url: "signinout.php",
        data: "username=" + username,
        success: function() {
            $('.user').html('<span>Welcome <span id="loggedUser">' + username + '</span>!</span> <a id="signOut" onclick="window.location.reload()">SIGN OUT</a>');
    打赏 评论

相关推荐 更多相似问题