javascript - 未解析时为 undefined json,解析时为 undefined token

标签 javascript php json postgresql

在从 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/

相关文章:

javascript - 具有可抓取内容的 JS 应用程序(检测搜索机器人服务器端)?

php - LinkedIn 二级连接 API

java - 如何使用 Java 使用另一个 JSON 模式验证 JSON 模式结构?

javascript - php 中的 Json_encode 并在 jquery 中检索它

javascript - Highchart - 将更多系列添加到多个同步的 Highstock 图表之一

javascript - 动态检查元素的高度

javascript - 从子文件夹运行 eslint 时无法解析相对模块路径

javascript - HTML + IE6 + IE7 + IE8 + IE9, 不可点击的div元素

PHP 依赖和@package-annotations

javascript - 我们可以在 javascript url 中传递 GET 变量吗?