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.

5
  • This reads as if you are using a "GET" request to update your database? Commented Apr 24, 2018 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. Commented Apr 24, 2018 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) Commented Apr 24, 2018 at 14:55
  • you didn't change the URL ('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=.....); Commented Apr 24, 2018 at 15:00
  • Your URL is too long, see stackoverflow.com/questions/417142/… Commented Apr 25, 2018 at 18:35

1 Answer 1

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) );
Sign up to request clarification or add additional context in comments.

4 Comments

I've tried with post too, please se the question edited. @Quentin
@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.
@Quentin tryed now but its not adding the parameter in the url
@KevinRodriguez — Not adding the parameter in the URL is the point!

Your Answer

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