javascript - 无法在 JSON 字符串上使用 .indexof,因为它没有方法 'indexof' javascript

标签 javascript json jsonp

我已经解析了一些 JSON,但我想获取从 JSON 获得的链接,在链接中找到一个使其不同的标识符(例如 www.foo.com/IDENTIFIER/home),并拥有该标识符作为一个字符串插入到另一个使用标识符的函数中。

这是我尝试使用 indexOf 查找前置标识符路径索引的代码,这样我就可以在它之后获取标识符的索引(该代码尚未编写,因为我被困在这里)。这也使用 JSONP。

$.jsonp({
    "url": "http://foo.com&callback=?",
    "data": {
    "alt": "json-in-script"
    },
    "success": function (data) {
        var link = data[0].link;
        var jsonText = JSON.stringify(link);

        // the below code returns string
        console.log('jsonText string?'+typeof jsonText); 

        // the below code returns that this method has no object indexOf
        var index = jsonText.indexof("pre-identifier-path/"); 

    },
    "error": function () {
    }
});

感谢您抽出宝贵时间,如果这有点令人困惑,我们深表歉意。

最佳答案

该方法应命名为 indexOf ,区分大小写。例如:

jsonText.indexOf("pre-identifier-path/"); 

关于javascript - 无法在 JSON 字符串上使用 .indexof,因为它没有方法 'indexof' javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7108252/

相关文章:

javascript - 将新行 ' ' 自动添加到 select 选项

c# - JSON 反序列化为泛型

jQuery ajax 请求使用 jsonp 错误

jquery - jsonp不一致错误: object is not a function

javascript - 调试用 Testcafe 编写的 UI 测试

javascript - 增加 div 的高度,只有它下面的 div 向下移动,而不是整行

javascript - 如何从服务器发送和接收数据: python/flask/ajax/json GET POST requests

jQuery .ajax 和同源策略

javascript - 尝试使可拖动的表单元素水平 jQuery

javascript - 获取json,图片url