假设我有一个 URL http://example.com/product/12345其中包含代码行。该 URL 是电子商务网站的产品页面,该页面内有一个“添加到购物车”按钮。
单击该按钮时,它会发送有关所查看产品的 HTTP POST 并返回以下 JSON 响应:
{
"products": [
{
"id": 722,
"link": "http://example.com/product/123123",
"image_link": "http://example.com/prodcut/image/thumb.jpg",
"image_width": "45",
"image_height": "45",
"quantity": 1,
.... and so on...
我想知道是否可以在不知道哪个函数调用它的情况下解析响应,因为:
- 该页面由多个 View 组成(从不同的 Controller 呈现)
我尝试过做以下事情:
console.log(response.products),或者; console.log(产品)
但都给出了“未定义”错误...
有人可以帮我解决这个问题吗?非常感谢。
最佳答案
如果有帮助的话,我可以为您指出确切的文件:
- /themes/yourtheme/js/modules/blockcart/ajax-cart.js 如果不存在,请查看/modules/blockcart/ajax-cart.js
看注释行下面的代码“//发送ajax请求到服务器”
- 在
/themes/yourtheme/modules/blockcart/
或/modules/blockcart/< 中的 Smarty 模板文件
blockcart-json.tpl
中形成 JSON 答案
所以,如果你将console.log放在ajax-cart.js中
success: function(jsonData,textStatus,jqXHR)
{
console.log(jsonData.products); // not parsed json here of course
...
},
它应该可以工作。
关于javascript - 从 HTTP 响应中解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35368544/