开发者

Replace is not defined, but why?

开发者 https://www.devze.com 2023-01-19 05:07 出处:网络
can somebody tell me what is wrong? JS Code $.ajax({ url:\"http://www.google.com/complete/search?qu=chicken\",

can somebody tell me what is wrong?

JS Code

$.ajax({
    url:"http://www.google.com/complete/search?qu=chicken",
    success:function(data){

        var test_data = ''+data+''; // convert object to a string
        $('body').append(typeof(test_data));

        var test_data = replace.test_data(/[0-9]/,'X');
        $('body').append('<hr />'+test_data+' <hr />');

    },
    dataType:'jsonp',
    error:function(){
        alert('error');
    }
});

jsfiddle http://www.jsf开发者_StackOverflow中文版iddle.net/V9Euk/664/

Thanks in advance!

Peter


You got it backwards; it should be

test_data.replace(...);

Also, you don't need var before the second assignment to "test_data"; just the first one.


replace is undefined. It is not an object.

Use replace from the string prototype

"string".replace(//, "");

As you can see:

alert(String.prototype.replace)

Results in

function replace() {
    [native code]
}


You should replace replace.test_data by test_data.replace

0

精彩评论

暂无评论...
验证码 换一张
取 消