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: https://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 :)

Aotik
  • 131
  • 1
  • 8
  • Thats why I'm trying to figure out why I'm getting the error. Same comes up with an ajax request – Aotik Jul 26 '15 at 19:49
  • 406 response means that `content-type` in your response is not acceptable to the browser. See the `Accept` request header for your Ajax call and see if the `content-type` response header matches with one of the values in your request – Arkantos Jul 26 '15 at 19:50
  • @arkantos I see that the content length being sent is more than 'accepted' I guess? Here is a screenshot http://i.imgur.com/SSczVGa.png – Aotik Jul 26 '15 at 19:56
  • @vinayakj, tried that again, didnt work. – Aotik Jul 26 '15 at 19:56
  • @vinayakj.. 406 is more about some invalid response type that is not acceptable which is making the request. I think using $.ajax directly will not make any difference as $.post internally uses $.ajax – Arkantos Jul 26 '15 at 19:57
  • content-length is not an issue here, it's just the size of your request and response. Actual issue here is `charset`. As you see in your screenshot, request headers `charset=UTF-8` but response headers `charset=iso-8859-1` which is not understood by the browser. Hence the error while parsing the response. – Arkantos Jul 26 '15 at 20:03
  • Hmm, interesting. I've tried set `contentType: "charset=iso-8859-1;"` in an Ajax request, but nothing actually gets stored through php – Aotik Jul 26 '15 at 20:09
  • just setting by content-type header in your Ajax request will not help, It's the browser which is making the actual request, so your browser should be able to understand and interpret the response. You need to change your `charset` in response headers. Look at my answer for more details – Arkantos Jul 26 '15 at 20:12

1 Answers1

1

Web browsers make a request for information from the server. When this happens, it sends an Accept header. This tells the server in what formats the browser can accept the data. If the server cannot send data in a format requested in the Accept header, the server sends the 406 Not Acceptable error.

From the screenshot attached in the comments, you can see clearly that charset in response headers is iso-8859-1. Just send the response in UTF-8 encoding and that should solve the issue.

Have a look at this SO link for setting charset header in PHP response.

Community
  • 1
  • 1
Arkantos
  • 6,530
  • 2
  • 16
  • 36
  • Awesome thanks. I've tried setting `header('Content-Type: charset=utf-8');` to both index.php & store.php however I'm still getting a 406 :/ Response header still comes up as `Content-Type:text/html; charset=iso-8859-1` – Aotik Jul 26 '15 at 20:16
  • did you verify the same in your response headers ? like you did earlier in dev tools – Arkantos Jul 26 '15 at 20:17
  • Yeah, however when I send small blocks of text, the response header is actually: `Content-Type:charset=utf-8` – Aotik Jul 26 '15 at 20:19
  • Hmm I won't be of much help unless I have a closer look at your code. Unfortunately you don't even have enough rep to discuss this over chat – Arkantos Jul 26 '15 at 20:26
  • Since its a small project, I uploaded it to pastebin: http://pastebin.com/5R0wKteU http://pastebin.com/4Ncq8P1g and the site is http://notes.hkbot.io/ If you'd like to have a look, that'd be awesome, thanks :) – Aotik Jul 26 '15 at 20:30
  • Steps to replicate the issue in your site ? – Arkantos Jul 26 '15 at 20:35
  • Adding a small block of text such as "ok here is a block of text" will go through, but a larger block, such as the code in pastebin will bring up the error, then clicking options (top right) then save – Aotik Jul 26 '15 at 20:37
  • In your site, I can still see the charset as `iso-8859-1` not `UTF-8`. You have this line `header('Content-Type: charset=utf-8');` in your PHP file while it should be `header('Content-Type: text/html; charset=utf-8');`, You forgot the actual text/html content type value, charset is additional info about the response encoding.. see if that helps – Arkantos Jul 26 '15 at 20:44
  • Still bringing up the error when saving a block of code, but when it is a long block of plain text, it doesnt get saved by sql but doesnt bring up an error – Aotik Jul 26 '15 at 20:47
  • Did you make those changes in your site ? I still see `iso-8859-1` instead of `utf-8` at http://notes.hkbot.io :) – Arkantos Jul 26 '15 at 21:01
  • Yeah lol, added `header('Content-Type: text/html; charset=utf-8');` to both store.php and index.php – Aotik Jul 26 '15 at 21:02
  • Wooow mate. I found the problem. Textarea cant process – Aotik Jul 26 '15 at 21:20
  • Cool.. You found the issue on your own :-) – Arkantos Jul 27 '15 at 05:00