使用包含撇号的值调用REST

REST Call with values containing apostrophe

本文关键字:值调用 REST 包含撇      更新时间:2023-09-26

我使用REST和ajax从SharePoint获取数据使用上面的:

https:xxxxxxxx/_vti_bin/ListData.svc/RMSD_Tasks?$orderby=IssueValue asc,StatusValue desc&$filter="+dropValue+" eq '"+secondFilterVal+"'&groupby=IssueValue/StatusValue

一切都很好,除了一个情况。有时,secondFilterVal的值是带撇号的"Manager's Bulletins"。当我打电话时,我收到了一条糟糕的请求信息。我在这里能做什么?不幸的是,文字"经理公告"不能更改。我正在考虑使用"$filter=substringof.....",但它与其他一切都很好地配合使用。Thansk !

感谢@Lauri和@charlietfl的评论。@charlietfl给我的信息特别有用,因为它让我看到了不同的选项,而encodeURIComponent()是我要走的路。阅读MDN的encodeURIComponent()和这个REST过滤器的链接帮助我解决了使用上述代码的问题:

由于某些奇怪的原因,您需要两次撇号:结果Manager%27%27s%20Bulletins工作完美。

var str = fixedEncodeURIComponent("Manager's Bulletins");
function fixedEncodeURIComponent(src) {
    return encodeURIComponent(src).replace(/[!'()*]/g, function (c) {
        return '%' + c.charCodeAt(0).toString(16) + '%' + c.charCodeAt(0).toString(16);
    });
}