0

I need to pass a json variable as a paramater to a php script that will process the json data and store it in Database.

So first, in javascript, i was testing sending data like this :

$('#sendResult').load('http://localhost/myurl/phpFile.php?mrData=' + jsonArrFinal);

This was working well when passing small records (records can vary, it depends the data that user insert).

But when i increased the records, it started appearing this error in console:

414 (Request-URI Too Long)

I've changed the js code to:

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/myurl/phpFile.php?mrData=' + jsonArrFinal );
xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
xhr.send();

But still appearing the same error with POST method. I've checked the json param and it has 5439 characters.

How can i resolve this? Thanks in advance.

Please note that the length can be 8x more than 5439 characters.

  • This reads as if you are using a "GET" request to update your database? – Julian Reschke Apr 24 '18 at 14:52
  • 1
    You are passing data in the URL which has a maximum size. Use a POST request and put the data in the body. – B3rn475 Apr 24 '18 at 14:52
  • @JulianReschke yes but then i've changed the script to a POST method but still the same error. (i've made an edit to my question, adding this modification) –  Apr 24 '18 at 14:55
  • you didn't change the URL ('http://localhost/myurl/phpFile.php?mrData=' + jsonArrFinal) so you have the same problem , in POST you have to send jsonArrFinal in this way http.send(?mrData=.....); – Luca Olivieri Apr 24 '18 at 15:00
  • Your URL is too long, see https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers – rob006 Apr 25 '18 at 18:35

1 Answers1

1

Don't use a GET request.

You're storing data, so you should be using a POST request anyway.

Use $.post instead of $.load and write your own logic to display the response in the done() handler.


I've changed the js code to:

You need to put the data in the body of the request. POST requests don't change the rules for how much data you can put in the URL.


$.post("http://localhost/myurl/phpFile.php", { mrData: jsonArrFinal })
 .done( data => $("#sendResult").html(data) );
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I've tried with post too, please se the question edited. @Quentin –  Apr 24 '18 at 14:53
  • @KevinRodriguez yeah, you used POST, but you are still appending it as a GET parameter on the querystring. It needs to be in the body like the example in the answer above. – epascarello Apr 24 '18 at 14:57
  • @Quentin tryed now but its not adding the parameter in the url –  Apr 24 '18 at 15:06
  • @KevinRodriguez — Not adding the parameter in the URL **is the point**! – Quentin Apr 24 '18 at 15:06