0

I have the following Ajax request

var csrfName = '<?php echo $this->security->get_csrf_token_name();?>';
var csrfHash = '<?php echo $this->security->get_csrf_hash();?>';
var searchTerm = $("#mastheadSearch").val();
var data = {};
data['Search'] = searchTerm;
data[csrfName] = csrfHash;
$.ajax({
    type: 'POST',
    url:'<?=base_url()?>news/support',
    contentType : 'application/json',
    data:JSON.stringify(data),
});

But I can not access this information from the controller.

$rawData = file_get_contents("php://input");
$newData = json_decode($rawData);

The strange thing is that if I insert a breakpoint with xdebug, it says the variable $newData has a value (which is the value I expected it to be). However if I try and access this variable within my code I get an error message (Severity: Notice

Message: Trying to access array offset on value of type null).

I have read posts of people with similar issues and tried the suggested fixes but none seem to work for me.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Chris S
  • 29
  • 1
  • 5
  • Have you tried `$_POST['data']` instead of `file_get_contents`? I've never seen that other method, although I'm no PHP expert, and it may be perfectly valid. The only other thing I could think of is that $newData variable isn't in scope when you're trying to use it, but you didn't provide enough example PHP for me to know. – Sean Kendle Sep 27 '21 at 15:21
  • 2
    Which line produces the error? Also add `var_dump($newData);` to see what actually is returned by json_decode. – Daniels118 Sep 27 '21 at 15:22
  • 1
    @SeanKendle $_POST doesn't work for data sent in JSON format (such as in this case). See https://stackoverflow.com/questions/18866571/receive-json-post-with-php – ADyson Sep 27 '21 at 15:24
  • I tried $_POST['data'] originally but I read that using file_get_contents("php://input") may solve the issue (it didn't). – Chris S Sep 27 '21 at 15:26
  • var_dump($newData) is returning NULL – Chris S Sep 27 '21 at 15:26
  • @ADyson that's strange, I do it every day – Sean Kendle Sep 27 '21 at 15:29
  • Ok, go back a step then. Try `var_dump($rawData);` and see what you get. You need to trace it to the point where it starts to fail. Also, monitor the Ajax request using your browser's network tool and check what is actually being sent inside the request body – ADyson Sep 27 '21 at 15:29
  • 1
    @SeanKendle in that case you're probably sending form data rather than JSON – ADyson Sep 27 '21 at 15:29
  • var_dump($rawData) is returning string(0) "" but if I insert a breakpoint with xdebug it says $rawData = {"Search":"search","ci_csrf_token":""} – Chris S Sep 27 '21 at 15:32
  • Are you sure you're checking it at the right time then? Obviously if you look during the main request to load you're page then it would be empty, you need to check during the Ajax request. Output from the Ajax request will be visible in your browser's network tool, or you can surface it using the JavaScript which handles the response to the Ajax request (which i see you haven't yet written) – ADyson Sep 27 '21 at 15:34
  • @ADyson you're right! Wow, never realized it. I'm sending a raw object in via jQuery and never set my contentType, and I don't JSON.stringify the object. It works, though, so good enough for me, haha! Learn something new every day. – Sean Kendle Sep 27 '21 at 15:34
  • 1
    @SeanKendle yeah jQuery will automatically serialise a raw object into form-url-encoded format if you don't specify otherwise – ADyson Sep 27 '21 at 15:35

0 Answers0