0

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.

4
  • Does $Data have value? Commented Jun 19, 2017 at 6:29
  • @aldrin27 How to see $Data has a value or not ? Commented Jun 19, 2017 at 6:33
  • print_r($Data); exit; below $Data Commented Jun 19, 2017 at 6:34
  • Nothing happen after I add print_r @aldrin27 Commented Jun 19, 2017 at 6:43

1 Answer 1

4

when you send json data to php its in doesn't come in $_POST or $_GET its in php://input strem;

the ajax request u send ws not jQuery its core js, which is good but its quite unflexible and tends to break in different browser. i just used the jQuery version of the ajax which is very flexible and cross-browser as well.

try this : JS:

$.ajax({
      method:'POST',
      contentType:'application/json',
      url:'http://example.com/share/new',
      data: JSON.stringify({"id": "1", "type": "new", "data": "testabcd"}),
      success:function(response){
       console.log(response);
      }

   });

PHP:

public function reservation()
{

    $Data = json_decode(file_get_contents('php://input'), true);

    $data_share = array(
        'id' => $Data['id'],
        'type' => $Data['type'],
        'data' => $Data['data']);

    $this->db->insert('mytable', $data_share);

    return "200";
}
Sign up to request clarification or add additional context in comments.

19 Comments

It's better than my code but I still got NULL data when insert to my database. It's mean I still cannot get the data.
since you were using core js i hv converted to jquery ajax, i think it should work now
Sorry, I don't really understand with converted to jquery ajax. Could you explain more ? Thanks
i mean the ajax request u send ws not jQuery its core js, which is good but its quite unflexible and tends to break in different browser. i just used the jQuery version of the ajax which is very flexible and cross-browser as well.
Could you give me some example ?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.