如何正确传递jquery的.ajax()函数的datastring参数?(how do i pass datastring parameter for jquery's .ajax() fu

编程入门 行业动态 更新时间:2024-10-22 04:57:10
如何正确传递jquery的.ajax()函数的datastring参数?(how do i pass datastring parameter for jquery's .ajax() function properly?) var username = $('#username').val(); var dataString = 'username=' + username; $.ajax({ 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>'); } });

使用上面的代码,我的用户名变量没有正确传递,我假设我编码datastring参数的方式有问题,但我不知道如何正确地做到这一点。

下面是我在signinout.php中使用的php代码,用于将用户名插入数据库,没有输入用户名字段,每个新条目都进入数据库。

$username = protect($_POST['username']); $time = time(); $sql = "INSERT INTO users (username, join_date) VALUES ('$username', '$time')"; $result = mysqli_query($cn, $sql) or die(mysqli_error($cn)); var username = $('#username').val(); var dataString = 'username=' + username; $.ajax({ 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) VALUES ('$username', '$time')"; $result = mysqli_query($cn, $sql) or die(mysqli_error($cn));

最满意答案

您的“最佳”数据字符串取决于您在服务器端部分的需求。 例如,这个jquery-ajax调用将一个对象发送到服务器端操作(PHP):

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

因此,在您的服务方面,您可能拥有此脚本,该脚本将返回与您发送的相同:

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

每种数据的输出(参见mydata变量)是:

案例 :mydata =“hellostring = 1”;

Array( [hellostring] => "1" )

这意味着,在服务器端,您可以:

$_123 = $_POST["hellostring"];

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

结果,你得到:

Array ( [title] => some [value] => thing )

所以你可以:

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

Case mydata = [1,2,3];

惊喜,这不起作用,:),你应该用这种形式包装它:

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

因此,您可以像上一个案例一样在服务器端继续。

注意:

为避免被黑客攻击:( PHP CASE示例)使用以下方法过滤输入: http://php.net/manual/es/function.filter-input.php

更多

为了在服务器端部件中进行更高级的数据处理(即:在接收ajax请求的脚本中),您可以通过以下方式使用json:

首先假设你是通过javascript发送一个对象:

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

并且,在您的服务器端:

<?php // 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" ?>

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]; $.ajax({ cache: false, type: 'post', async: true, data: mydata, url: 'some-script.php', success: function(resp){ console.log("OK",resp); }, error: function(e){ console.log(e.responseText); } });

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

// some-script.php <?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:

Array ( [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.

Note:

To avoid get hacked: (PHP CASE example) filter your input using: http://php.net/manual/es/function.filter-input.php

More

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" }; ..do your ajax call stuff here..

And, in your server side:

<?php // 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" ?>

更多推荐

本文发布于:2023-08-05 05:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   如何正确   参数   ajax   jquery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!