There is a html page from which you need to send a very long string of PHP characters to a handler on another domain via $ .ajax. The string will constantly increase over time and at some point may already have tens or even hundreds of thousands of characters. I tested and found out that the length of the URL in the Ajax request can be about 4800 characters , more than Ajax does not miss, at least in Chrome. The first thing that comes to mind is to break the entire line into parts no larger than 4800 and send all the parts alternately, at some interval, and then in the handler to collect everything back into a single whole. Other solutions are welcome.

My request for Ajax:

$('button').click(function() { var veryLongString = 'Very long stringggggggggggg'; $.ajax({ method: 'GET', url: 'http://mydomain.com/myhandler.php', dataType: 'jsonp', crossDomain: true, jsonpCallback: 'myCallback', data: {key: veryLongString}, success: function(data) { console.log(data); } }); }); 

To break apart as elements of an array found (again, other ideas are welcome):

 veryLongString.match(/.{1,4800}/g); 

And what to do next - I do not know. Like through setInterval () or how?

ps. How to collect on the side of the handler while the question does not bother, while only how to send in parts (who knows who knows, maybe you need to immediately take the handler into account ...)

When switching to method: 'POST'

I try to send about 12,000 characters, but still I get the same error in the console (with the word GET for some reason) and the request is not sent. Mistake:

 GET http://mydomain.com/myhandler.php?callback=jQuery111103417691031936556_1…20%25D0%25BE%25D1%2581%25D0%25B5%25D0%25BB%25D1%258F%2522)&_=1445978068736 send @ jquery-latest.min.js:4 m.extend.ajax @ jquery-latest.min.js:4 (anonymous function) @ mypage.html:2014 m.event.dispatch @ jquery-latest.min.js:3 r.handle @ jquery-latest.min.js:3 

Line 2014 is $.ajax({

  • one
    Use POST and do not need to invent crutches. - Dmitriy Simushev
  • one
    You will not believe, but for POST requests there are no limits per line. (Rather, they are, but you are very unlikely to be rested against them). - Dmitriy Simushev
  • In fact, you did not switch to POST. If you really want to use JSONP, POST will not help you. - Dmitriy Simushev
  • I need to transfer data to another domain (more precisely, from the local HTML page to the online server) so I found JSONP as a solution, otherwise I get the error No 'Access-Control-Allow-Origin' header is present... And crossDomain: true, also not helps - stckvrw
  • Hmm ... then you can either look in the direction of POST + CORS or wait for someone to propose a solution to your original problem (with data partitioning) - Dmitriy Simushev

2 answers 2

Use POST request instead of GET request. Although, there is a limit on the length of the body during a POST request, you will almost certainly not resist it (usually, the maximum request size is measured in megabytes). To send an AJAX POST request using jQuery, you can use the following code:

 $.ajax({ method: 'POST', url: 'http://mydomain.com/myhandler.php', dataType: 'json', data: {} // Другие параметры запроса... }); 

UPD :

If you really want to use JSONP, then you will not be able to transfer data through POST requests. JSONP just doesn't work that way, alas.

  • And what is the limit for a POST request? - stckvrw
  • Depends on web server settings. In practice, this value has a megabyte order. - Dmitriy Simushev
  • As far as I know, this does not depend on the browser (at least I have never encountered such problems). You understand that a request with a body of 1 megabyte can contain a string of a length of the order of a million characters. Do you really have such big requests? - Dmitriy Simushev
  • Something is still wrong. I tried to send a post-request: it skips 9,150 characters somewhere, but already 9,200 does not. At the same time on the server hosting provider is post_max_size 8MB ie should pass about 8 million. Where else can there be a limit? - stckvrw
  • See the logs of apache errors, there should be some information about the problem - Dmitriy Simushev

After you have broken a string into an array, you can send this array:

UPD:
Before sending the first part, you must declare a variable with the timestamp of sending the first piece and add it as a parameter to all parts of the message.

 var messageId = new Date().getTime() stringArray.each(function(index, item) { $.ajax({ method: 'GET', url: 'http://mydomain.com/myhandler.php?part='+ index + '&messageId=' + messageId, dataType: 'jsonp', crossDomain: true, jsonpCallback: 'myCallback', data: {item}, success: function(data) { console.log(data); } }); }) 

After sending, on the controller, collect the string.
You can use the usual for, in order to track the last element of the array and send the parameter last.
If you have problems with the order when you receive requests, you can add the async: false parameter to $.ajax .
PS: to increase the payload of the GET request, after debugging, you can shorten the parameter names.