Regex match from response using jQuery [duplicate]

问题内容:

This question already has an answer here:

I am trying to get the view-source of a website and then doing a regex match. I am using jQuery. I was testing the script in Google developer console. It is returning this error Cannot read property 'match' of undefined . . But it is working expected when I am entering individual command one by one.

q = "google.com";
var view_src = $.get('http://'+q, function(response) 
                                         { return response; 
                                         }
                           );
       var src_res = view_src.responseText;
       var reg_gg = src_res.match(/google.com/);
       console.log(reg_gg);

问题评论:

    
get is an asynchronous request. You have to work on the response inside the callback. Your callback return will never make it to view_src.

答案:

答案1:

It’s because your view_src, doesn’t contains reposneText property.

Try to move code with match to success function. Should help.

答案评论:

原文地址:

https://stackoverflow.com/questions/47754682/regex-match-from-response-using-jquery

添加评论

友情链接:蝴蝶教程