如何解析ajax响应并在html中使用它[关闭]

I am creating a liking and disliking system on my website. Whenever user will click on like button an ajax request will be made to increment in like of that particular post. Similarly when user click on dislike button it will increment in dislikes.

Now what i want is that i want to fetch the information for the post likes and dislike counts. For this purpose request will be made by ajax and after processing page will return this associative array. Now i want to use these values on my page returned by the remote page.

I want to know how can i let my php script to communicate with my Javascript client.

Thanks

Array ( [likes] => 4 [dislikes] => 5 [like_width] => 44 [dislike_width] => 55 [message] => [dislike_button] => [like_button] => ) 

You need to return data in JSON format, otherwise Javascript will not be able to parse the output.

Use json_encode($yourArray) to convert an array to a string in JSON format.

If you add the header to return the type "application/json" JavaScript just read an js object. If the return type is "text/html" (the default) you need to convert the sctring to JSON using JSON.stringify(data).