0

I'm using jquery and jason sending data to another contoller, but when I send the data, I check it in fire bug and it's fine, It sends it correctly but in the target page, when I var_dump the $_REQUEST or $_POST is returns null.

I'm using codeigniter by the way.

This my jquery code:

<script type="text/javascript">
function GetMovieLanguageCategory(Language) 
{
 $.ajax({
  type: "POST",
  contentType: "application/json",
  url: "/admin/movie/get_language_category",
  data: JSON.stringify({"Language":Language}),
  success: function(Data)
  {
    alert(Data);
  },
  failure: function(ErrorMsg) {
  alert(ErrorMsg);
  },
}); 
}
</script>

And in my Controller:

var_dump($_REQUEST);
//var_dump(json_decode($_POST['Language']));

And it returns:

array(0) {}

Am I wrong somewhere?

7
  • I don't see anything wrong with your code that would cause the issue you are having. Note however, failure should be error Commented Dec 20, 2013 at 20:41
  • Do you have any other scripts like this that accept json strings via post? I wonder if there's some setting or .htaccess entry messing with your requests. Commented Dec 20, 2013 at 20:43
  • No, I tried contentType: "application/json" but didn't help! Commented Dec 20, 2013 at 20:47
  • I'm using codeigniter, Is anything about htaceess in that about jason? Commented Dec 20, 2013 at 20:48
  • 1
    $_REQUEST only gets populated when you send a query string as either POST or GET. You are not doing that. You need to read the raw POST body: $postBody = file_get_contents('php://input'); echo $postBody;. Commented Dec 20, 2013 at 21:12

1 Answer 1

3

You are sending JSON data, but marking it as application/x-www-form-urlencoded data and you are trying to parse it as application/x-www-form-urlencoded data.

Change:

data: JSON.stringify({"Language":Language}),

to

data:{"Language":Language},

and let jQuery encode it properly for you.


If you want to encode it yourself (don't!):

data: "Language=" + encodeURIComponent(Language);

If you really want to send JSON:

contentType: "application/json",
data: JSON.stringify({"Language":Language}),

then, in the PHP, get the body of the request and run it through json_decode.

Sign up to request clarification or add additional context in comments.

7 Comments

If that is the issue, using contentType: "application/json" would allow him to access it via $_REQUEST wouldn't it? i was under the impression that jquery would send it in the request body, and php would give you access to it via $_REQUEST
But I want to send data Using jason, I'm Using compression and If I don't jason encode it, it won't work
@KevinB — No. PHP will never automatically decode JSON requests.
no, not auto decode, i expected it to give you the raw json, which it does as you've pointed out, just not in $_REQUEST
@MasOud — I've updated the answer with instructions on how to make a JSON request. (Note "JSON" not "jason", it isn't a singer-songwriter who dated Kylie). I don't see how this would make a difference to being able to compress it though.
|

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.