从URL

2017-08-05 34 views
-1
获取回复

我有以下JS代码来获取来自URL的响应。我的网址没问题,我收到了我想要显示的回复,但我不知道如何以JSON格式显示回复。我会感谢任何帮助。从URL

var id = '84337255-f472-4630-986d-487f22009536'; 
var startDate = '2017-01-01'; 
var endDate = '2017-03-30'; 

var url = 'http://34.209.24.195/facturas\?id\=' + id + '\&start\=' + startDate + '\&finish\=' + endDate; 

var urlRequestTimeout = setTimeout(function() { 
    console.log("Failed to load resources"); 
}, 8000); 

$.ajax({ 
    url: url, 
    dataType: "json", 
    method: "GET", 
    success : function(response) { 
    console.log(response); 
    clearTimeout(urlRequestTimeout); 
    } 
}); 
+0

你已经使用'console.log'在浏览器控制台中打印JSON。你想在DOM中显示结果吗? – Nisarg

回答

0

您可以使用chrome控制台查看您正在打印的响应。没有看到输出是什么,我只能猜测。

我会做这样的事情

let jsonObj = {}; //create empty JSON variable 

$.ajax({ 
     url: url, 
     dataType: "json", 
     method: "GET", 
     success : function(response) { 
     console.log(response); 
     jsonObj = response.data; //set the variable to the response data 
     clearTimeout(urlRequestTimeout); 
     } 
    }); 

现在你已经响应保存,你可以用它做你想要什么。如果你想要一个更具体的答案,你必须发布你的回应输出。

或者,正如其他人所说的那样。您可以将值转换为JSON字符串像这样:

let jsonObj = JSON.stringify(response.data) 
console.log(jsonObj); 
+0

这是我得到的输出:[回复](http://imgur.com/a/eFzxY) – ArCiGo

+0

@ArCiGo,这是正确的回应? – 23k

+0

不,正确的回答必须是“干预100个结果”或“Faltanparámetros”,或该id的发票总数。这是[链接](http://34.209.24.195/facturas?id=84337255-f472-4630-986d-487f22009536&start=2017-01-01&finish=2017-03-30)其中一个正确的输出 – ArCiGo

0

使用

console.log(JSON.stringify(response.data)); 

,而不是

console.log(response); 
0

你收到什么是JSON格式。

我认为,这是解决 1. JSON.parse() 2. eval("(" + response + ")") 但方法第二种方法将运行在JSON代码(不安全)

json format:var data='{ 
    "student" : [ 
     {"name":"A","age":17}, 
     {"name":"B","age":17}, 
     {"name":"C","age":17} 
    ] 
}' 

eval('(' + data + ')');//jsonString->jsonObject 
JSON.parse(data);//jsonString->jsonObject