-3

数据字符串是:

{"feedback_type":"000","error_type":"","textarea":"blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah"}

我使用以下代码发布到 PHP:

// ajax post
$.ajax({
    type: "POST",
    url:  "core/poster.php" ,
    data:  dataString,
    success:function()
    {
        alert("Success!");
    }
});

return false;

和 php 文件:

<?php

require 'Class_DBOperation.php';
require 'global.php';

// Establish Database Connection
$dbOperation = new class_DBOperation(DBHOST,DBUSER,DBPWD,DBNAME,DBCHARSET);

// Receive dataString
$content=$_POST['feedback_type'];
$run=mysql_query("insert into reports values (NULL, '".$content."')");

?>

问题是为什么 $content 是空的?我应该怎么办 ?有任何想法吗?

4

4 回答 4

2

在你的成功函数中添加一个响应并提醒它

$.ajax({
    type: "POST",
    url:  "core/poster.php" ,
    data:  dataString,
    success:function(response)
    {
        alert(response);
    }
});

并在您的 poster.php 文件中尝试将以下内容添加到 PHP 标记的顶部。

ini_set("display_errors", 1);
var_dump($_POST);

这应该为您提供一个开始和调试正在发生的事情的地方。

于 2012-06-05T17:53:20.647 回答
0

删除参数名称的双引号

{
   feedback_type: "000",
   error_type: "",
   textarea: "blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah"
}
于 2012-06-05T17:56:14.790 回答
0

您正在发送一个 JSON 字符串作为参数字符串。

参数格式应如下:

foo=bar,foo2=bar2,foo3=bar3ETC...

您可以重新格式化字符串以遵循规范:

JS

var dataString = "feedback_type=000&error_type=&textarea=blahblahblah";

PHP

echo $_POST['feedback_type']; // 000
echo $_POST['error_type']; // null
echo $_POST['textarea']; // blahblahblah

或者您可以将 JSON 字符串作为 POST 参数传递:

JS

var jsonObject = {
    "feedback_type" : "000",
    "error_type" : "", 
    "textarea" : "blahblah"
}


var jsonString = '{"feedback_type":"000","error_type":"","textarea":"blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah"}';

// OR

var jsonString = JSON.stringify(jsonObject);

var dataString = "json_string=" + jsonString;

PHP

// String - suitable for database input
echo $_POST['json_string']; // String: {"feedback_type":"000","error_type":"","textarea":"blahblahblahblahblahblahblahblahblahblahblahblahblahblahblahblah"}

// Parse into array
$json_array = json_decode($_POST['json_string']);
于 2012-06-05T20:56:57.693 回答
0

这不是一个直接的解决方案,但它可以帮助您找出问题所在。尝试转储 $_POST 超全局的内容,这将通知您数据是如何接收的。尝试类似:

print '<pre>';
print_r ($_POST);
print '<pre>';
于 2012-06-05T17:52:00.400 回答