【发布时间】:2018-03-15 12:35:17
【问题描述】:
在从 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 查询,然后我用
发送它回显 json_encode($nouvelle);
启动动作 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),我会收到以下错误消息:
Uncaught (in promise) SyntaxError: Unexpected token o in JSON at position 1
如何进行设置,以便可以使用 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
}
【问题讨论】:
-
你使用的是哪个 Javascript 库?
-
那是无效的 json JSON validator
-
我正在使用 Vue.js
-
使用您的验证器的错误消息是:错误:第 1 行的解析错误:“{”id”:1872,“title ---^ Expecting 'EOF', '}', ':' , ',', ']', 得到'未定义'
-
@wzirmer 是的,这是因为开头和结尾的引号
标签: javascript php json postgresql