I am using a Laravel blade.
My code below happens error when the size of item of textarea is huge. I am looking for a way to solve it.
Everyone. How should I send a huge text to server? How should I modify my codes?
Blade(JavaScript)
function sendByGet()
{
var items = document.getElementById("item").value;
var param = "?items="+items+"&id={{$id}}";
var url = {{(url)}} + encodeURI(param);
let result = fetch(url);
result.then(response => response.json()).then(
responceObject =>{
}
}
}
Controller PHP
public function receivebyGet(Request $request)
{
$lineArray = array();
$slipData = explode("\n", $request->items);
Error
the date is replaces <huge_text> (Actual data is text(5000 characters))
phpbladeName?id=01:270 GET https://test.com/test/send_by_get?&item=<huge_text> 414 sendByGet @ confirmation?id=01:270 onclick @ confirmation?id=0:244 VM142:1 Uncaught (in promise) SyntaxError: Unexpected end of JSON input at phpbladeName?id=01
function sendByGet(url) {
const items = document.getElementById("item").value;
const param = encodeURI("?id={{$id}}");
fetch(url + param, {
method: 'GET',
headers: { 'Content-Type': 'plain/text' },
body: items,
})
.then(response => response.json())
.then( ... );
}
PHP Controller (assuming being Laravel)
public function receivebyGet(Request $request) {
$lineArray = array();
$slipData = explode("\n", $request->getContent());
...
}
As mentioned by Peter Krebs the maximum URL size (which includes the Query) is 2000 characters. So you cannot expect your system to reliably work if url is longher than 2000. Read more here.
As pointed out by Jabaa you should semantically choose the method, not technically. But this is something that has evolved over time (initially the GET body was supposed to be rejected/ignored) read more here. Hence you should consider it carefully and verify that all the parties involved (server, browser, proxies, cache) supports it properly. This is why oftentimes developers choose to violate semantics and use a POST method.