Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何清理要插入我的Carto表的用户输入数据?_Javascript_User Input_Cartodb - Fatal编程技术网

Javascript 如何清理要插入我的Carto表的用户输入数据?

Javascript 如何清理要插入我的Carto表的用户输入数据?,javascript,user-input,cartodb,Javascript,User Input,Cartodb,我有一个应用程序,可以将用户提供的地理数据和用户输入的注释插入到Carto表中。其工作原理是通过q参数传递一个完全格式的PostgreSQL查询如何在javascript中构造查询字符串,通过POST调用传递给API,以便单引号不会打断查询? 目前我正在做一些类似的事情 var sql = "SELECT insert_data('" + user_input +"');"; $.ajax({type: 'POST', url: 'https://' + config.carto

我有一个应用程序,可以将用户提供的地理数据和用户输入的注释插入到Carto表中。其工作原理是通过
q
参数传递一个完全格式的PostgreSQL查询如何在javascript中构造查询字符串,通过POST调用传递给API,以便单引号不会打断查询?

目前我正在做一些类似的事情

var sql = "SELECT insert_data('" + user_input +"');";
$.ajax({type: 'POST',
        url: 'https://' + config.cartoUsername + '.carto.com/api/v2/sql', //Sending the data to Carto
        crossDomain: true,
        data: {"q": sql},
        dataType: 'json',
        success: function(responseData, textStatus, jqXHR) {
                            console.log("Data saved");
                        },
        error: function(responseData, textStatus, errorThrown) {
                            console.log("Problem saving the data");
                        }
                    });
如果我提交类似
我在这里
的内容,那么我会得到一个400错误

{"error":["syntax error at or near \"m\""]}
因为查询看起来像:

SELECT insert_data('I'm here');

有没有方便的前端javascript库可以像这样清理SQL输入?

试试这种格式的
用户输入

var user_input = "I am her";
console.log("SELECT insert_data(" + user_input +");");

此查询将如下所示:
SELECT insert\u data(我在这里)

目前,我的解决方案是使用and将任意数量的单引号加倍,并使用JSON.stringify()清除双引号

JSON.stringify(user_input.replace(/'+/g, "''"))

这不起作用,因为sql函数的输入必须是“单引号字符串”