[英]Parsing JSON from HTTP response
假设我有一个包含代码行的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...
我想知道是否可以解析响应而不知道哪个函数调用它,因为:
我试图做类似的事情:
console.log(response.products),或; 执行console.log(产品)
却都给出“未定义”错误...
谁可以帮我这个事 ? 非常感谢。
我可以为您指出确切的文件,如果有帮助的话:
看起来像注释行下面的代码“ //将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
...
},
它应该工作。
使用JSON.parse(response)转换为Json对象并再次转换为字符串使用JSON.stringify(response)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.