dtye7921 2016-06-21 11:31
浏览 19

如何解析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] => ) 
  • 写回答

1条回答 默认 最新

  • dongxian6715 2016-06-21 11:46
    关注

    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).

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?