最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Why are integers becoming strings when I POST using jquery.ajax to a PHP script - Stack Overflow

matteradmin1PV0评论

I have spent the last hour attempting to figure this out to no avail. There are many posts on SO about jQuery and ajax(), but I haven't been able to find one that deals with my specific issue.

The basics of my code:

On the client:

var data = {"id": 1};
j.ajax({
  type: "POST",
  url: "postTestingResult.php",
  data: {'data': data},
  dataType: "json",
  success: ajaxSuccess,
  error: ajaxError
});

On the server using PHP:

$data = $_POST['data'];
echo $data; //{"id": "1"}

Why is the integer value being a string? How do I prevent this? I really don't want to have to create a custom function to loop through my data object (which in reality is quite plex) to convert all the values.

Many thanks!

I have spent the last hour attempting to figure this out to no avail. There are many posts on SO about jQuery and ajax(), but I haven't been able to find one that deals with my specific issue.

The basics of my code:

On the client:

var data = {"id": 1};
j.ajax({
  type: "POST",
  url: "postTestingResult.php",
  data: {'data': data},
  dataType: "json",
  success: ajaxSuccess,
  error: ajaxError
});

On the server using PHP:

$data = $_POST['data'];
echo $data; //{"id": "1"}

Why is the integer value being a string? How do I prevent this? I really don't want to have to create a custom function to loop through my data object (which in reality is quite plex) to convert all the values.

Many thanks!

Share edited Apr 4, 2014 at 4:25 Matthew Herbst asked Apr 1, 2014 at 7:28 Matthew HerbstMatthew Herbst 32.1k26 gold badges90 silver badges137 bronze badges 2
  • 2 Try apply JSON.stringify on data variable: data: {'data': JSON.stringify(data)} – hindmost Commented Apr 1, 2014 at 7:34
  • see my ment on the answer by @mesutozer – Matthew Herbst Commented Apr 1, 2014 at 8:10
Add a ment  | 

2 Answers 2

Reset to default 6

When parameters are sent through $_GET or $_POST , They are interpreted as strings.

You may need to cast them appropriately to make them suit how it works for you.

In your case, you need to json_decode the data you received. Then you will have variables in their the type you sent via Ajax.

$data = json_decode($_POST['data'], true);
// $data['id'] is now int

$_POST['data'] is a string as explained in other answers, but when you decode it you can inner elements get their proper types.

Post a comment

comment list (0)

  1. No comments so far