0

I'm trying to post large amounts of text via $.post in jQuery and getting a 406 response. It works fine under around 300 characters. Below is my code:

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>

Getting the following response: http://i.stack.imgur.com/MUZpX.png

Have also tried with a local form submit, but it would also bring up a bad request.

Note: All whitespace and formatting is preserved when stored, and that is intentional

If anyone could help me out that would be great, thanks :)

4

1 回答 1

1

Web 浏览器向服务器发出信息请求。发生这种情况时,它会发送一个Accept标头。这告诉服务器浏览器可以接受哪些格式的数据。如果服务器无法以Accept标头中请求的格式发送数据,则服务器发送406 Not Acceptable error.

从评论中附带的屏幕截图中,您可以清楚地看到charset响应标头是iso-8859-1. UTF-8只需以编码发送响应,这应该可以解决问题。

查看此SO链接以在 PHP 响应中设置字符集标头。

于 2015-07-26T20:10:23.350 回答