简体   繁体   English

console.log 对 Ajax 成功不起作用

[英]console.log does not work on Ajax success

I'm working on a website on Wordpress,我在 Wordpress 的网站上工作,

I've this function in PHP:我在 PHP 中有这个 function:

add_action( 'wp_ajax_myfunction', 'myfunction' );
add_action( 'wp_ajax_nopriv_myfunction', 'myfunction' );
function myfunction() {

    require_once(ABSPATH."private/classes/utils.class.php");

    header('Content-Type: application/json');

    $listeLocalisation=Utils::listeLocalisation($_POST['localisation']);

    echo json_encode($listeLocalisation);
}

In my JS file:在我的 JS 文件中:

jQuery(".localisation").change(function () {
  jQuery.ajax({
    type: "POST",
    data: { action: "myfunction"},
    url: adminAjax,
    success: function (data) {
      console.log('TEST');
    }
  });
});

When I'm inspecting my console, I've receive the good data but my console.log() does not work.. nothing is displayed.当我检查我的控制台时,我收到了良好的数据,但我的 console.log() 不起作用.. 没有显示任何内容。

Thanks for your help !谢谢你的帮助 !

JS is a real pain when you have to debug, but maybe try the followings:当您必须调试时,JS 是一个真正的痛苦,但也许可以尝试以下方法:

  1. Add this after the success:成功后添加:
error: function(){
   console.log("Some message to display when the error happens");
}

and see what happens.看看会发生什么。 If you see the error message in the console, then there's a problem with your ajax request.如果您在控制台中看到错误消息,那么您的 ajax 请求存在问题。

  1. Disable cache in your browser, delete cookies and restart your browser.在浏览器中禁用缓存,删除 cookies 并重新启动浏览器。

  2. Sometimes the same thing happens to me and if I'm 100% sure I've made no mistake, then... well... it sound unlogical and silly, but delete your whole ajax code and type down (or copy-paste) the same thing again.有时同样的事情会发生在我身上,如果我 100% 确定我没有犯错,那么......好吧......这听起来不合逻辑和愚蠢,但删除你的整个 ajax 代码并输入(或复制粘贴) 同样的事情。 I know, I know, but sometimes it works for me.我知道,我知道,但有时它对我有用。

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

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