19

i need to POST a huge string large about 3mb, is it possible to send it to php using not url params?

If i send it in url params the request reachs the size limit in url.

How can work around this? any clue?

thanks a lot.

Actually i'm doing it like this:

$.ajax({
 type:'POST',
.......
data:{string:3MB_string}
});

i'm using PHP and jQuery , and i wouldl ike to send the 3mb base64 string to php on a simple url, like site.com/script.php

The string is a File Reader API base64 image

this is an example of string but this won't reach the size limit it is not a 3mb is less cause troubling in pasting that to show you a 3mb, http://jsfiddle.net/QSyMc/

bombastic
  • 2,961
  • 8
  • 20
  • 18

5 Answers5

28

You will need to use a POST request:

$.ajax({
    url: '/script.php',
    type: 'POST',
    data: { value: 'some huge string here' },
    success: function(result) {
        alert('the request was successfully sent to the server');
    }
});

and in your server side script retrieve the value:

$_POST["value"]

Also you might need to increase the allowed request size. For example in your .htaccess file or in your php.ini you could set the post_max_size value:

#set max post size
php_value post_max_size 20M
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 4
    i'm already sending that in this way :P @Darin Dimitrov and this cause url size limit reached – bombastic Jul 23 '13 at 12:24
  • i mean i get console "you reached request limit" – bombastic Jul 23 '13 at 12:24
  • @bombastic We're going to need more information then. What is the `URL` you are sending it too? What server side language is it using? etc. – crush Jul 23 '13 at 12:25
  • 4
    You might need to increase the maximum allowed request size as well. I have updated my answer to illustrate that. – Darin Dimitrov Jul 23 '13 at 12:26
  • 1
    @DarinDimitrov link for your answer: http://www.php.net/manual/en/ini.core.php#ini.post-max-size – crush Jul 23 '13 at 12:28
  • +1 @DarinDimitrov sorry but data:{} is the playload? it doesn't puts params in the post url then? – bombastic Jul 23 '13 at 12:28
  • @bombastic `data` is the data sent to the `send(data)` method of the [`XmlHttpRequest`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) object, which is the payload. – crush Jul 23 '13 at 12:30
  • @crush has that somenthing to do with url params in a POST ? – bombastic Jul 23 '13 at 12:32
  • 1
    @bombastic I'm explaining why it's not sent in the URL params. The URL is sent in the `open()` method, not the `send()` method. When you specify `type: 'POST'` in jQuery, you are telling it that you want to send the `data` as the payload, not as url params. – crush Jul 23 '13 at 12:35
  • this is the string returned from php http://jsfiddle.net/QSyMc/ it is not 3mb troubling with copy and paste too huge a 3mb string – bombastic Jul 23 '13 at 12:37
11

Try with processData to false and a string representation of your JSON

var data = { "some" : "data" };
$.ajax({
    type: "POST",
    url: "/script",
    processData: false,
    contentType: 'application/json',
    data: JSON.stringify(data),
    success: function(r) {
       console.log(r);
    }
});

From the jQuery.ajax documentation :

processData (default: true)

Type: Boolean

By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.

krampstudio
  • 3,519
  • 2
  • 43
  • 63
3

You definitely need to use POST method. If the string is still to large, check you php.ini file to find out the maximum POST parameter size.

To change this value value, do any one of those:

1. change values in php.ini

post_max_size=20M
upload_max_filesize=20M

2. or add this code to .htaccess file

php_value post_max_size 20M
php_value upload_max_filesize 20M

Which one to use depends on what you have access to.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Artem Kiselev
  • 643
  • 1
  • 7
  • 7
3

I came across a similar problem. It wasn't the length of the query string but rather the number of variables I was passing to the server. The php.ini places a limit in the max_input_vars field to 1200 variables. In my case, I was exceeding that amount rather than the post_max_size amount. Had to go back and make the query more efficient and under the limit. I guess I could have raised the php.ini setting but I wound up with better code by disabling non-essential query parameters.

jdp
  • 356
  • 2
  • 8
0

Try this:

var str = "YOUR STRING";
$.ajax({
    url: 'YOUR URL',
    type: 'POST',
    data: { mystring: str },
    success: function(result) {
        console.log(result);
    }
});
Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35