最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting an error "has no method 'contains'" - Stack Overflow

programmeradmin4浏览0评论

I am trying to use the following code snippet:

console.log(config.url);
if (config.method === 'GET' && config.url.contains("/Retrieve")) {

It outputs:

/Content/app/home/partials/menu.html app.js:255
TypeError: Object /Content/app/home/partials/menu.html has no method 'contains'
    at request (http://127.0.0.1:81/Content/app/app.js:256:59)

However this is giving me and error with the word ".contains". Anyone have any idea why I am getting this message?

has no method 'contains'

Does anyone have any idea why I might be getting this message?

I am trying to use the following code snippet:

console.log(config.url);
if (config.method === 'GET' && config.url.contains("/Retrieve")) {

It outputs:

/Content/app/home/partials/menu.html app.js:255
TypeError: Object /Content/app/home/partials/menu.html has no method 'contains'
    at request (http://127.0.0.1:81/Content/app/app.js:256:59)

However this is giving me and error with the word ".contains". Anyone have any idea why I am getting this message?

has no method 'contains'

Does anyone have any idea why I might be getting this message?

Share Improve this question asked Feb 13, 2014 at 11:57 Samantha J T StarSamantha J T Star 33k89 gold badges257 silver badges441 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 8

Because in JavaScript, strings don't have a contains method. You can use indexOf:

if (config.method === 'GET' && config.url.indexOf("/Retrieve") !== -1) {

Note that that's case-sensitive. To do it without worrying about capitalization, you can either use toLowerCase():

if (config.method === 'GET' && config.url.toLowerCase().indexOf("/retrieve") !== -1) {

...or a regular expression with the i flag (case-Insensitive):

if (config.method === 'GET' && config.url.match(/\/Retrieve/i)) {

The problem is that contains is a jQuery function so you can apply it only on jQuery objects.

If you want to use pure javascript you have to use indexOf function

contains() is not a native JavaScript method; it's used by JS libraries such as jQuery though.

If you're just using pure JavaScript then you could use a method such as indexOf().

发布评论

评论列表(0)

  1. 暂无评论