I have a problem to send the JSON data from JQuery with GET request. This is my JQuery to send the data with GET.
var xhr = new XMLHttpRequest();
var url = "http://example.com/share/new?data=" + JSON.stringify({"id": "1", "type": "new", "data": "testabcd"});
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
}
};
xhr.send();
This is in my Controller file.
public function share()
{
header('Content-type: application/json');
$Data = json_decode($_GET["data"]);
$data_share = array(
'id' => $Data['id'],
'type' => $Data['type'],
'data' => $Data['data']);
$this->db->insert('mytable', $data_share);
return "200";
}
The problem is nothing grab in the Controller and the insert query doesn't insert anything. How to fix this problem ? Maybe I do something wrong in my code ? Thank you before.