gpt4 book ai didi

jQuery $.post() 疑问!

转载 作者:行者123 更新时间:2023-12-01 08:26:31 25 4
gpt4 key购买 nike

我正在尝试使用 POST 将变量从 try.htm 传递到 chat.php

try.htm 的代码是:

<head>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type = "text/javascript">
function yo() {
var text = $("#msg").val();
$.post("chat.php",msg:text);
}
</script>
</head>
<body>

<input type="text" id="msg" onkeyup="yo()">
<div id="display">Change</div>
</body>

chat.php 的代码是:

<?php
$msg=$_POST['msg'];
mysql_connect("localhost","root");
mysql_select_db("user");
mysql_query("INSERT INTO user (name,pwd,status) VALUES ('$msg','work','0')") or die(mysql_error());
?>

问题是“msg”变量似乎没有传递到chat.php!怎么了?

最佳答案

将其更改为:

$.post("chat.php", { msg:text } );

jQuery 期望数据作为对象传递,而 { ... } 本质上会为我们创建一个匿名对象。 msg:text——没有大括号——不幸的是,它没有做太多事情,并且会在运行时抛出错误。

因此,将两者放在一起:{ msg:text } 创建一个匿名对象,其属性为 msg,并填充 text 的值> 变量。

关于jQuery $.post() 疑问!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3405628/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com