Ajax array value display error

I am basic ajax and am using display result (form and custom value (like Json)) ajax. My code:

var obj  = {"employees":[
   {"firstName":"John", "lastName":"Doe"},
   {"firstName":"Anna", "lastName":"Smith"},
   {"firstName":"Peter", "lastName":"Jones"}
]};
var data = $("#userForm").serialize() + "&jsonval=" + obj;                   
 $.ajax({
                    datatype : "json",
                    type: 'POST',
                    url: 'all.php',
                    data: data,
                    })

      

My result:

Array
(
    [firstname] => frtr
    [lastname] => dfgfdg
    [email] => praneshkanna@gmail.com
    [num] => 2323232323
    [num1] => 34334
    [num2] => 2342
    [num3] => 2432
    [submit] => Submit
    [jsonval] => [object Object]
)

      

Jsonval -display result [object Object] .plz help with display result all employees (first and last name). Thanks for your feature and correct my mistake.

+3


source to share


2 answers


Since you are trying to post JSON Object in post column, so you will need to change Object to string

Change

var data = $("#userForm").serialize() + "&jsonval=" + obj;   

      



to

var data = $("#userForm").serialize() + "&jsonval=" + JSON.stringify(obj);   

      

Now you will get the JSON content to a string on the server, so extend the value jsonval

on the server side

+3


source


Obj is now an object that is converted to a String with the default toString, which is "[Object Object]" in the case of an object. Instead you have to convert it to JSON String with JSON.stringify like this



JSON.stringify(obj)

      

+1


source







All Articles