Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax array value display error

Tags:

json

html

ajax

I am basic ajax and 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] => [email protected]
    [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 name and Last name). Thanks for your feature help and correct my fault.

like image 788
Kannan Avatar asked Dec 13 '25 09:12

Kannan


1 Answers

Because you are trying to post JSON Object in post string, so you will need to change Object into string

Change

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

to

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

Now you will get JSON content into string on server, so decode jsonval field value on server side

like image 109
Girish Avatar answered Dec 16 '25 05:12

Girish