dousuowu4610 2019-06-20 13:11
浏览 101

将字符串化的JSON返回给PHP

I'm using a JQuery script manipulate an image. So far, all the functions are working as intended, except for returning the data to PHP. I've tried several examples that I've found on Stackoverflow, yet I've been unable to return the data without any errors.

Note: JavaScript is not my primary language, my previous attempts at using Ajax have failed.

    $.ajax({
        type: "POST",
        url: "/foo/bar.php",
        datatype: "json",
        data: { 
            submit: JSON.stringify(format(), null, 2) 
        },
        cache: false,
        success: function() {
            console.log("Order Submitted");
        }
    });

This is the error that is generated:

designer.js:276 Uncaught TypeError: Cannot read property 'slice' of undefined
at format (designer.js:276)
at designer.js:340

Slice is a function inside Format. At the end of the Format function, it returns a variable out. I've attempted to replace the data with data: { test: out }, but this proved to be unsuccessful as well.

In PHP, I call the result as follows; $stringData = $_POST['submit']; Which returns the following: Array ( [submit] => Submit )

When console.log(out) is called without the ajax code in place, it returns the values correctly. As soon as the ajax code is added, parts of the script stop operating as intended.

function format() {
var all = rects.slice().sort(function(d, f){ return d[0] - f[0]});
var lst = null;
var out = [];

for (var i = 0; i < all.length; i++) {
        var cur = all[i];
        var pen = cur[0];

        if (lst != pen) {
                var locs = [];

                out.push(locs);
                lst = pen;
            }
            var ecl = cur[1].pos;
            var pos =
            {
                pos: [
                    round(ecl[0] / size.x),
                    round(ecl[1] / size.y),
                    round(ecl[2] / size.x),
                    round(ecl[3] / size.y)
                    ]
            };

        if(typeof cur[1].colour !== 'undefined'){
                pos.colour = cur[1].colour;
            }

        if(typeof cur[1].filter !== 'undefined'){
                pos.filter = cur[1].filter;
            }
    locs.push(pos);
}
console.log(JSON.stringify(out, null, 2));
return out;

}

  • 写回答

1条回答 默认 最新

  • duanchun1881 2019-06-20 14:47
    关注

    Generally, the error Cannot read property 'slice' of undefined implies you're calling .slice() on something that doesn't exist.

    The first line of your format function does make a rects.slice() - however you do not show where this rects variable comes from (it seems to be global).

    Given this is the only call to slice in the code you show, what is probably happening is that the global variable rects is indeed undefined.

    Adding console.log(rects) as the first line of the format function will help you debug it.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图