帮助理解 jQuery $.post - 带代码示例

发布于 2024-09-15 02:04:35 字数 844 浏览 4 评论 0原文

$.post('testeUpdate.php', 'autocomplete',
        function(dadosResposta) {

            $('#ns-nome').val(dadosResposta.nsName);
            $('#ns-endereco').val(dadosResposta.nsAddress);
        },
    "json");

我正在尝试理解这一点。因此,我附近有 jquery $.post 参考链接:

1) 一个 post 请求发送到 testeUpdate.php,然后,我们可以传递一个名为 'autocomplete' 的字符串。精确的?

问题1) 使用该字符串传递 post 请求意味着我们稍后可以,例如, 根据条件请求的过程具体指向: $_POST['自动完成']; ?

2) 后来,我们有一个“成功时”回调函数,它接受一个参数,dadosReposta。精确的?

问题2) 这个dadosResposta 可能来自我们的服务器端脚本?是 这是一个包含我们收到的数据的参数?

3) 因此,成功后,我们要做的是:用一些值填充某个输入元素。 val(dadosResposta.nsName);

问题 3) 这种“点表示法”是一种访问 json 格式数据的方法吗?还是像我们通常那样在 DOM 上行走的一种方法?是这样,那么json在这里扮演什么角色呢?

预先非常感谢, MEM

$.post('testeUpdate.php', 'autocomplete',
        function(dadosResposta) {

            $('#ns-nome').val(dadosResposta.nsName);
            $('#ns-endereco').val(dadosResposta.nsAddress);
        },
    "json");

I'm trying to understand this. So, and having the jquery $.post reference link near me:

1) A post request is send to testeUpdate.php, then, we can pass a string called 'autocomplete' . Precise?

Question 1)
Passing a post request with that string, means that we can later, for example,
process that request upon a conditional by specifically point to:
$_POST['autocomplete']; ?

2)
Later, we have a "on success" callback function what accepts a parameter, dadosReposta. Precise?

Question 2)
This dadosResposta is something that may come from our server side script? Is
this an argument having the data we receive?

3) So, on success, what we want to do is: populate some input element with some values. val(dadosResposta.nsName);

Question(s) 3)
is this "dot notation" a way to access data on json format - or is this a way to walk on the DOM as we normally do? Is so, then what role is json playing here ?

Thanks a lot in advance,
MEM

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-09-22 02:04:35

1) 是的,那么可以通过这种方式使用 PHP 访问自动完成功能。 ($_POST['自动完成'])。但除了在 isset() 上显示 true 之外 - 它不会有任何数据。

2) 是的,dadosResposta 是将从服务器返回的响应。如果您在 $.post 请求中将最后一个参数设置为 json,则它可以在 javaScript 中作为 json 对象本地使用。要以这种方式显示数据,在 PHP 中,您可以使用 json_encode()< /a>

3) 一旦你的数据进来了,你就可以做你想做的事了。但是点符号只有在 json 格式正确的情况下才有效。参考: json.org

4) 点表示法是一种方法访问的是json格式返回的数据,与DOM无关。如果将 $.post 的最后部分更改为“文本”,则可以将服务器返回的数据作为常规字符串进行处理。

1) Yes then autocomplete can be accessed using PHP this way. ($_POST['autocomplete']). But other than the fact that it will show true on isset() - it won't have any data.

2) Yes dadosResposta is the response that will come back from the server. If you have set the last parameter as json on your $.post request, it can be used natively in javaScript as a json object. To display data in this way, in PHP you can use json_encode()

3) You can do as you please once your data comes in. But the dot notation will only work if the json is formatted properly. Ref : json.org

4) Dot notation is a way to access the data returned in the json format, has nothing to do with the DOM. If you change the last portion of your $.post to "text" the data returned from the server can be worked upon as a regular string.

梦初启 2024-09-22 02:04:35
  1. 是的,这是正确的,它是唯一的 post 变量,例如您正在检查 isset($_POST['autocomplete']),尽管您似乎希望实际在此处传递文本框值,因为自动完成通常依赖于您已经输入的内容。
  2. 是的,这个函数在响应返回时运行,参数是从 PHP 页面返回的任何数据。
  3. 响应应该看起来像这样:
    { nsName: '名称', nsAddress: '地址' }
    它使用这些值来填充这 2 个字段,因此点符号从响应中获取值,JSON 只是让这变得更清晰:)
  1. Yes this is correct, it's the only post variable, e.g. you're checking isset($_POST['autocomplete']), though it seems you'd want to actually pass the textbox value here, since autocomplete normally relies on what you've typed already.
  2. Yes this function runs when the response comes back, the argument is whatever data comes back from your PHP page.
  3. The response is supposed to look something like this:
    { nsName: 'name', nsAddress: 'address' }
    It's using these values to populate those 2 fields, so the dot notation is getting the values from the response, JSON is just making this much cleaner :)
森林散布 2024-09-22 02:04:35

我在 jsFiddle 上使用了您的代码,并尝试在您拥有的基础上进行构建,如您所见,我将数据发送到jsFiddle 上的 json echo 函数,它的响应如下(可能......如果是 PHP):

echo json_encode(array("post_response"=>$_POST));

如您所见,我更改了 postData 以匹配您请求的响应,在服务器端,它通常看起来有点像这样正常情况:

if(isset($_POST['somedata']))
{
    //do stuff... you know, whatever
    echo json_encode(array("nsName" => $someString1,"nsAddress" => $someString2));
}

I used your code on jsFiddle and tried to built on what you have, as you can see I sent data to the json echo function on jsFiddle, which responded like this (probably... if its PHP):

echo json_encode(array("post_response"=>$_POST));

As you can see I changed the postData to match the response you were requesting, on the server side it would usually look a bit like this in a normal scenario:

if(isset($_POST['somedata']))
{
    //do stuff... you know, whatever
    echo json_encode(array("nsName" => $someString1,"nsAddress" => $someString2));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文