简体   繁体   English

如何使 jQuery ajax 调用在浏览器控制台中产生响应?

[英]How can I make jQuery ajax calls produce a response in the browser console?

I'm caching a number a links in a database via a page crawler that makes ajax calls with jQuery.我正在通过使用 jQuery 进行 ajax 调用的页面爬虫在数据库中缓存一个链接。 Before making an ajax call to a remote site, I make sure the link address has one of a list of domains or has one of two file extensions:在对远程站点进行 ajax 调用之前,我确保链接地址具有域列表之一或具有两个文件扩展名之一:

var j = jQuery.noConflict();

...

var substrings = ['site1',
                  'site2',
                  'site3',
                  'site4'];
var length = substrings.length;
console.log(length);
$.each(the_links, function(i, el){
  var dl_link = this;
  if (dl_link.endsWith('.html') || dl_link.endsWith('.php') || dl_link.indexOf(substrings[length])!=-1) {
    j.get( 'https://example.com/insert.php', { title:post_title, link: dl_link } )
        .done(function( data ) {
          console.log('response:');
          afterInsert(data);
        });
  }
  length--;
  if(link_count == 0){
    console.log('inserted all links');
  }
});

When I run the script, my ajax calls run without issues and my records are created in the database, but nothing is returned to the browser console in the ajax callback:当我运行脚本时,我的 ajax 调用运行没有问题,我的记录在数据库中创建,但在 ajax 回调中没有任何内容返回到浏览器控制台:

浏览器终端输出

How can I access the contents of my ajax response?如何访问我的 ajax 响应的内容?

I also tried the following ajax call and I'm still not able to log the response in my console, even though it appears in the Response tab of the XHR entry in the console:我还尝试了以下 ajax 调用,但我仍然无法在控制台中记录响应,即使它出现在控制台中 XHR 条目的Response选项卡中:

j.get(insert_script, { title: post_title,link: dl_link}, function(data, textStatus, jqXHR) {
   console.log('response:'); 
   afterInsert(data);
});

It turns out my ajax response wasn't being provided to the console because of CORS .事实证明,由于CORS ,我的 ajax 响应没有提供给控制台。 I was able to get around this using the CORS Everywhere add-on for Firefox我能够使用 Firefox 的CORS Everywhere附加组件解决这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM