【问题标题】:At JSON.stringify , parsererrorSyntaxError: Unexpected end of JSON input在 JSON.stringify 中,parsererrorSyntaxError: Unexpected end of JSON input
【发布时间】:2020-02-02 09:33:37
【问题描述】:

在 JSON.stringify ,我的 javascript 在控制台中给出以下消息。

parsererrorSyntaxError: JSON 输入意外结束

var contentData =     {
                "Location": {
                    "City": "Bangalore Urban",
                    "State": "Karnataka",
                    "Country": "IN"
                },
                "Packages": [
                    "3 days and 4 nights"
                ],
                "Hotels": "Vividus",
                "Offers": [
                    "Enjoy up to 20% Savings",
                    "Hotel Discounts, Just for Booking Early.2 nights B&B to include first night dinner"
                ],
                "Activities": [
                    "scuba diving",
                    "swimming"
                ]
            }




        $.ajax({
            type : 'POST',
            url : '/my/reco/pkg.',
            data : JSON.stringify(contentData),
            async : false,    // I get this message both with false and true here.
            contentType : "application/json; charset=utf-8",
            success : function(data) {
                 console.log(data);

            },
            error : function(jqXHR, textStatus, message) {
                console.log("error"+jqXHR+textStatus+message);
            }
        });

当我在 chrome 中打开调试器时,我在 JSON.stringify 处看到错误。

我已经验证了 json 和 javascript 代码,两者在语法上似乎都是正确的。

我可以在 chrome 的 header 部分下的网络选项卡中找到请求 json。

我在这里做错了什么或错过了什么?

【问题讨论】:

  • contentData 是什么样的?服务器是否还会返回实际的 JSON 或 404 页面?
  • 请关闭服务器并再次运行它可能会有所帮助。
  • 为什么不将它作为 JSON 发送并添加 dataType: 'json'
  • 如果服务器端有模型,则无需对数据进行字符串化
  • @ElyasEsna 但请求甚至没有到达服务器端。

标签: javascript jquery ajax aem aem-6


【解决方案1】:

您不需要在JSON.stringify(contentData) 中执行JSON.stringify。你可以只发送一个对象。所以,不用它发送数据。

【讨论】:

    【解决方案2】:

    您没有将对象分配给变量 contentData

    的错误

    首先var contentData =

    var contentData = {
      "Location": {
        "City": "Bangalore Urban",
        "State": "Karnataka",
        "Country": "IN"
      },
      "Packages": [
        "3 days and 4 nights"
      ],
      "Hotels": "Vividus",
      "Offers": [
        "Enjoy up to 20% Savings",
        "Hotel Discounts, Just for Booking Early.2 nights B&B to include first night dinner"
      ],
      "Activities": [
        "scuba diving",
        "swimming"
      ]
    }
    
    
    
    console.log(JSON.stringify(contentData));

    【讨论】:

      猜你喜欢
      • 2021-11-06
      • 2016-10-21
      • 2019-08-31
      • 2017-06-24
      • 2018-02-19
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多