0

I have the following code:

var arr = {City:'Moscow', Age:25};
$.ajax({
 url: "<? echo $this->createUrl('cities/index');?>",
 type: "POST",
 data: JSON.stringify(arr),
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 async: false,
 success: function(data){
 alert(data);
 }
});

The result is null. In the PHP side I have:

echo json_encode($_POST);

and

print_r($_POST);

But both are giving empty results (checked Firebug also).

Sharanya Dutta
4,0412 gold badges20 silver badges29 bronze badges
asked Mar 27, 2014 at 5:23
1
  • 3
    Remove JSON.stringify Commented Mar 27, 2014 at 5:25

4 Answers 4

3

You can also set the dataType in Ajax to specify the content type as follows.

 var city='city name';
 var age=10; 
 $.ajax({
 url: "<? echo $this->createUrl('cities/index');?>",
 type: "POST",
 data:"City="+city+"&Age="+age,
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 async: false,
 success: function(data){
 alert(data);
 }
 });

and in cities/index.php you can get this data by follows

if($_POST){
 $city=$_POST['City'];
 $age=$_POST['Age'];
 // and do with $city and $age what you want.
 //for return anything thing to `json` use follows we pass $age back to ajax
 echo json_encode($age);
 }
answered Mar 27, 2014 at 5:49

Comments

1

I guess you don't need to stringyfy the data because data should be PlainObject or String but in your case you can simply write like below

var arr = {City:'Moscow', Age:25};
 $.ajax({
 url: "<? echo $this->createUrl('cities/index');?>",
 type: "POST",
 data: arr,
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 async: false,
 success: function(data){
 alert(data);
 }
 });

as documented in jquery official site https://api.jquery.com/jQuery.ajax/

data

Type: PlainObject or String

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

answered Mar 27, 2014 at 5:26

3 Comments

I have one more question. I replaced object to array, var arr= [1, 2, 3]; ... and .. data: JSON.stringify(arr).. but it is not sending, I tried without stringfy also.
check this out it might help you stackoverflow.com/questions/8890524/… @SarvarNishonboyev stackoverflow.com/questions/11455000/…
I got it, it helped so much. great!
1

The data option passed to $.ajax() must be either a simple object, that jQuery will transform into a string of the formatkey1=value1&key2=value2.. OR it should be a string of the form key1=value1&key2=value2...

In your case, it can be solved by passing the object itself and letting jQuery do the query string formatting:

$.ajax({
 ...
 data: arr,
 ...
});
answered Mar 27, 2014 at 5:25

Comments

0

try this

var arr = {City:'Moscow', Age:25};
 $.ajax({
 url: "<? echo $this->createUrl('cities/index');?>",
 type: "POST",
 data: arr,
 contentType: "application/json; charset=utf-8",
 dataType: "json",
 async: false,
 success: function(data){
 alert(data);
 }
 });
answered Mar 27, 2014 at 5:26

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.