使用POST方法将json发送到php服务器时需要帮助

使用POST方法将json发送到php服务器时需要帮助,php,javascript,ajax,json,Php,Javascript,Ajax,Json,我试图通过POST方法将json数据从HTML表单发送回php服务器。这是我的密码。它在回调函数中转到失败块。Firebug控制台(ctrl+shift+J)显示无错误 <script> function ADDLISITEM(form) { var options = form.txtInput.value; options = JSON.stringify(options); var url = "conn_mysql.php" var request = null; req

我试图通过POST方法将json数据从HTML表单发送回php服务器。这是我的密码。它在回调函数中转到失败块。Firebug控制台(ctrl+shift+J)显示无错误

<script> 
function ADDLISITEM(form)
{ 
var options = form.txtInput.value;
options = JSON.stringify(options);
var url = "conn_mysql.php"
var request = null;
request = new XMLHttpRequest();
request.open("POST", url, true);
request.onreadystatechange = function(){
    if (request.readyState == 4) {
            if (request.status == 200) {
                alert(request.responseText);
        } else {
            alert(request.status); 
        }
    }
}
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.send("options=" + encodeURIComponent(options).replace(/%20/g, '+'));
}
</script>

函数ADDLISITEM(表单)
{ 
var options=form.txtInput.value;
options=JSON.stringify(选项);
var url=“conn_mysql.php”
var请求=null;
请求=新的XMLHttpRequest();
打开(“POST”,url,true);
request.onreadystatechange=函数(){
if(request.readyState==4){
如果(request.status==200){
警报(request.responseText);
}否则{
警报(请求状态);
}
}
}
setRequestHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
request.send(“options=“+encodeURIComponent(options).replace(/%20/g,“+”));
}
conn_mysql.php

<?php  
    $json = $_POST['options'];
    $options = json_decode($json);
    $username = "user";  
    $password = "********";  
    $hostname = "localhost";  
    $dbh = mysql_connect($hostname, $username, $password) or die("Unable to 
    connect to MySQL");  
    $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");
    $query1 = "INSERT INTO user_spec (options) VALUES ('$options')";
    mysql_query($query1);
    //if(!mysql_query($query1, $dbh))
    //{die('error:' .mysql_error());} echo'success';
    $query = "SELECT * FROM user_spec";  
    $result=mysql_query($query);     
    $outArray = array(); 
     if ($result)
     { 
       while ($row = mysql_fetch_assoc($result)) $outArray[] = $row; 
     } 
      echo json_encode($outArray);
?> 
您的请求显示“失败”,因为使用不同的
readyState
调用了多次
onreadystatechange
函数。这是一个改进的、缩进更好的版本:

request.onreadystatechange = function(){
    if (request.readyState == 4) {
        if (request.status == 200) {
            alert('http.responseText');
        } else {
            alert('fail'); // fails here
        }
    }
}
只有当到达
readyState
时,您才应该检查
状态

此外,在为URL分配参数时,应该使用来正确编码参数(例如,在解析器认为标记新键/值对开始的值中发送
&
时)。当使用
POST
作为方法时,您应该根据规范将
%20
的所有实例更改为
+
,并将数据作为参数发送到
send
函数,而不是将其连接到URL:

var url = "conn_sql.php";
…
request.send("options=" + encodeURIComponent(options).replace(/%20/g, '+'));
更新:要在PHP中处理发送的字符串,请使用,例如:



(另请参见)

您想让我们为您编写代码吗?+1 Richards,@Harmen Stackoverflow是一个帮助站点……了解您收到的请求的状态可能会有所帮助。如果不是200,那怎么办?404? 403? 500?thanX steve,我刚才在读你的评论之前做的&是404。php文件名中有一个错误。thanX againNo问题。很高兴你明白了。非常感谢马塞尔给你所有这些有价值的建议:-)现在开始工作了。还有一个php脚本名称错误。现在我必须在php的帖子中捕捉到这一点。如果我遇到问题,我想我必须开始新的问题。另外,它没有用http.responseText打印任何响应,我使用request.responseText&它开始打印输出。@exceptible:确实,我错过了那个问题,但是您也必须删除变量名周围的单引号。不管怎样,不客气。我很高兴你成功了。还有一个问题。“alert(request.responseText)”应该打印文本JSON.stringify(选项)我们正在将其发送到url,但它正在打印程序从同一php脚本使用getJson接收的数据。它昨天晚上被忽略了,因为我不希望它再次进入成功块,它将不会打印它假定的内容。@Exceptable:不,这不是真的。变量
request.responseText
应该打印从PHP脚本接收到的数据。
<?php
    $json = $_POST['options'];
    $options = json_decode($json);
    // now $options contains a PHP object
?>