【发布时间】:2015-07-26 19:44:32
【问题描述】:
我正在尝试通过 jQuery 中的 $.post 发布大量文本并获得 406 响应。它在大约 300 个字符下运行良好。以下是我的代码:
index.php
html
<form class="formss" action="index.php">
<textarea id="submittts" name="tts" spellcheck="false"></textarea>
</form>
jQuery
$('.save').click(function() {
$.post('store.php', $('.formss').serialize())
});
store.php
<?php
$tts = $_POST['tts'];
$texttostore = $tts;
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "notes";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO notes (code)
VALUES ('$texttostore')";
if ($conn->query($sql) === TRUE) {
echo stripslashes(str_replace('\r\n',PHP_EOL,$texttostore));
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
.htaccess
<IfModule mod_security.c>
SecFilterCheckURLEncoding Off
</IfModule>
得到以下响应: http://i.stack.imgur.com/MUZpX.png
也尝试过使用本地表单提交,但它也会带来错误的请求。
注意:存储时会保留所有空格和格式,这是有意的
如果有人可以帮助我,那就太好了,谢谢:)
【问题讨论】:
-
这就是为什么我想弄清楚为什么我会收到错误。同样提出了一个ajax请求
-
406 响应意味着您的响应中的
content-type是浏览器不接受的。查看您的 Ajax 调用的Accept请求标头,并查看content-type响应标头是否与您的请求中的值之一匹配 -
@arkantos 我看到发送的内容长度超过了我猜的“接受”?这是截图i.imgur.com/SSczVGa.png
-
@vinayakj,再试一次,没用。
-
@vinayakj.. 406 更多的是关于一些不可接受的无效响应类型,它正在发出请求。我认为直接使用 $.ajax 不会有任何区别,因为 $.post 内部使用 $.ajax
标签: javascript php jquery .htaccess http-status-code-406