在从 javascript 中的 SQL 查询获取我的 json 响应时遇到问题
这是我的javascript
setNouvelle: function(id, action){
this
.$http
.get('news.api.php?function=getNouvelle&id='+id)
.then(function(response){
if (action == 'modal') {
this.modal.nouvelle = response.data;
} else if(action == 'edit') {
this.nouvelle = response.data;
this.nouvelle.publication =
moment(this.nouvelle.publication).format('YYYY-MM-DD');
}
});
},
在 PHP 方面,我有一个使用 json_build_object 的 postgresql 查询,然后我用
发送它echo json_encode($nouvelle);
启动 Action setNouvelle 时,response.data 包含以下内容:
"{"id" : 1872, "title" : "Test Title", "publication" : "2017-09-05T00:00:00", "summary" : "This is a test summary ", "category" : "admin", "content" : "<h2 style=\"text-align: justify;\">This is the test content ", "relateddisciplines" : [{"id" : 2, "name" : "Men's", "code" : "men"}, {"id" : 3, "name" : "Women's", "code" : "wen"}], "athlete" : [{"id" : 37359, "firstname" : "Reb", "lastname" : "ANDRADE", "federation" : "POR"}, {"id" : 25224, "firstname" : "Paul", "lastname" : "BULA", "federation" : "FRA"}], "events" : [{"id" : 15191, "startevent" : "2017-09-01", "endevent" : "2017-09-03", "title" : "World Cup", "city" : {"name" : "Minsk", "country" : {"code" : "BLR"}}, "status" : "approved", "hasresults" : true, "disciplines" : [{"code" : "men"}, {"code" : "wen"}]}], "keywords" : [{"id" : 40, "value" : "Women's"}, {"id" : 49, "value" : "Men's"}, {"id" : 347, "value" : "World Cup"}, {"id" : 771, "value" : "Minsk"}]}"
但是我不能使用返回数据的各个部分,例如,用 this.nouvelle.summary 显示摘要
如果我尝试 JSON.parse(response.data),我会收到以下错误消息:
未捕获( promise )SyntaxError:位置 1 的 JSON 中的意外标记 o
我该如何设置才能使用 JSON 响应的各个属性
谢谢
编辑:
这是我的应用声明:
var app = new Vue({
http: { options: { emulateJSON: true, emulateHTTP: true }},
el: '#app',
data: {
nouvelle: {
id : "",
title: "",
summary: "",
content: "",
category: "",
publication: "",
press_release: "",
keywords: [],
athletes: [],
events: [],
relateddisciplines: [],
create_time: ""
},
nouvelles: [],
modal: {
nouvelle: {
id : "",
title: "",
summary: "",
content: "",
category: "",
publication: "",
press_release: "",
keywords: [],
athletes: [],
events: [],
relateddisciplines: [],
create_time: ""
}},
file: {},
addMode: true,
alert: {
cls: "hidden",
message: "",
time: 0
}
最佳答案
通过更改此行使其工作:
this.modal.nouvelle = response.data;
到
this.modal.nouvelle = JSON.parse(response.body.nouvelle);
没有解析正确的东西
关于javascript - 未解析时为 undefined json,解析时为 undefined token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46559394/