找不到发送帖子请求的方法

时间:2019-07-03 14:24:18

标签: javascript jquery

所以我的问题是我想获取stroke值作为发布请求发送,就像我的脚本当前在发布请求中使用points属性值... {{ 1}} <<类似于发布请求中的内容,但是我需要它来执行l: (JSON.stringify(line))值。因此,如果可以使用stroke

之类的东西

我想使线条与笔触颜色匹配,并以与线条一样的颜色发送请求。

笔触值位于该SVG的c: (JSON.stringify(stroke))属性内。这是一张图片,显示了我需要记录的内容,就像style一样。

enter image description here

是的,只需解析并像发送points值一样发送它即可。

我已经尝试过像points那样做一个const,但是没有运气。

LINES

2 个答案:

答案 0 :(得分:2)

在这里,您可以像这样在数组中搜索“笔画”:

DEMO

xhr = new XMLHttpRequest();
xhr.open("GET", "http://colorillo.com/blqu.inline.svg");
xhr.addEventListener("load", function() {
    const xmlDoc = new DOMParser().parseFromString(
        this.responseText.trim(),
        "image/svg+xml"
    );
    let output = [];
    // Get all polylines as strings
    let lines = xmlDoc.getElementsByTagName('polyline');
    // Loop over all lines
    for(let line of lines) {
        //Define more vars here...
        //Often times you can just use something like below and don't need to put it in the loop.
        let stroke = null; //See Bruce'es comment (rgb() as output): line.style.stroke;
        let opacity = line.style.opacity;
        let strokeWidth = line.style.strokeWidth;

        // Loop over all styles of this line (output same as input [hex])
        for(let style of line.getAttribute('style').split(';')) {
            // Get name & value
            let valueOffset = style.indexOf(':');
            // Check if name equal to 'stroke'
            let value = style.substr(valueOffset + 1).trim()
            switch(style.substr(0, valueOffset).trim()) {
                case 'stroke':
                    // Save stroke value
                    stroke = value;
                    // Break out of the loop (we don't have to search further)
                    break;
            }
        }
        output.push({
            l: (JSON.stringify(line.getAttribute('points').split(' ').map(pair => pair.split(',').map(Number)))),
            w: (strokeWidth),
            c: (stroke),
            o: (opacity),
            f: ("1"),
            _: ("false")
        });
    }
    $.post("/draw.php?ing=_index", output);
});
xhr.send();

希望这会有所帮助。 -思维

答案 1 :(得分:0)

我猜可能是因为您正在JSON对象中调用JSON.stringify()。尝试以下方法:

xhr = new XMLHttpRequest();
xhr.open("GET", "http://colorillo.com/blqu.inline.svg");
xhr.addEventListener("load", function () {
    const xmlDoc = new DOMParser().parseFromString(
        this.responseText.trim(),
        "image/svg+xml"
    );
    const Lines = Array.from(xmlDoc.getElementsByTagName('polyline'), pl =>
        pl.getAttribute('points').split(' ').map(pair =>
            pair.split(',').map(Number)));
    Lines.forEach(line => {
        console.log(JSON.stringify(line));
        $.post("/draw.php?ing=_index", {
            l: ("foo"),
            w: ("1"),
            c: ("#000000"),
            o: ("75"),
            f: ("1"),
            _: ("false")
        });
    });
});
xhr.send();
相关问题