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/

0

5 Answers 5

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
10
  • 4
    i'm already sending that in this way :P @Darin Dimitrov and this cause url size limit reached
    – bombastic
    Jul 23, 2013 at 12:24
  • i mean i get console "you reached request limit"
    – bombastic
    Jul 23, 2013 at 12:24
  • 4
    You might need to increase the maximum allowed request size as well. I have updated my answer to illustrate that. Jul 23, 2013 at 12:26
  • 1
    @DarinDimitrov link for your answer: php.net/manual/en/ini.core.php#ini.post-max-size
    – crush
    Jul 23, 2013 at 12:28
  • 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, 2013 at 12:35
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.

2
  • Can you add some explanation as to why you think this is necessary or why this would work?
    – crush
    Jul 23, 2013 at 12:36
  • var data = { "some" : "data" }; half json, half object?
    – Epirocks
    Dec 19, 2017 at 0:40
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.

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.

0

Try this:

var str = "YOUR STRING";
$.ajax({
    url: 'YOUR URL',
    type: 'POST',
    data: { mystring: str },
    success: function(result) {
        console.log(result);
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.