jQuery$.post()怀疑!

jQuery$.post()怀疑!,jquery,Jquery,我正在尝试使用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() {

我正在尝试使用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>

函数yo(){
var text=$(“#msg”).val();
$.post(“chat.php”,msg:text);
}
改变
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());
?> 
将其更改为:

$.post("chat.php", { msg:text } );
jQuery期望数据作为一个对象传递,而
{…}
实际上将为我们创建一个匿名对象
msg:text
——没有花括号——不幸的是,它做的不多,在运行时会抛出错误


因此,将两者结合起来:
{msg:text}
创建一个匿名对象,该对象的属性为
msg
,填充了
text
变量的值。

如果操作错误,请使用适当的JSON对象:

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

您忘记了$.post的数据参数周围的大括号

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

参数作为数组传递:

<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>

函数yo(){
var text=$(“#msg”).val();
$.post(“chat.php”,{msg:text});
}
改变

实际上,您应该得到语法/解析错误。它不是JSON对象。它是一个普通的旧文本JavaScript对象;)