Didn"t forge 2013-11-01 14:22 采纳率: 25%
浏览 331

将json对象保存到文件

I need to save a json object to a file. Therefore I'm using another PHP script which writes the data to a file. Data and filename are sent by an ajax POST request.

var results = $('results');
var filename = $('#filename').val();

$.ajax({
    url: 'jsonWriter.php',
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: JSON.stringify([{data: data, filename: filename }]),
    success: function(data) {
        results.html(data);
    },
    error: function(jqXhr, textStatus, error) {
        results.html("<p class=\"error\">ERROR: " + textStatus + ", " + error + "</p>");
    }
});

When I monitor the request in Fiddler, it looks fine and the posted data is correct. However, somehow the error function is invoked. It outputs

ERROR: parsererror, SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

I copied the posted JSON data from Fiddler into different online parsers, which ensured it is valid.

Why is the error function invoked anyway?

  • 写回答

1条回答 默认 最新

  • weixin_33716941 2013-11-01 14:27
    关注

    It looks to me like you are expecting the server to return text content, since you are applying it with .html:

    success: function(data) {
        results.html(data);
    },
    

    However, you are telling jQuery to expect JSON data:

    dataType: "json",
    

    jQuery is therefore attempting to parse your content returned from the server as JSON and failing. You need to change dataType to either text or html.

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集