简体   繁体   English

AJAX成功调用定义的函数

[英]AJAX Success call a defined function

I'm having trouble calling an external function on success. 我在成功调用外部函数时遇到麻烦。 The ideal solution would let me also pass in some parameters to the function! 理想的解决方案是让我还将一些参数传递给函数!

ie success: writeToConsole("successful"); success: writeToConsole("successful");

Javascript: Javascript:

$(document).ready(function () {
  $(".portalControls").find(".syncButton").click(triggerSync);    
});

function triggerSync(e) {
  e.preventDefault();
  $.ajax({
    type: "GET",
    url: "sync",
    dataType: "text",
    success: writeToConsole,
      error: writeToConsole
    }
  });
}

function writeToConsole(consoleText) {
  $.find("portalConsole").text(consoleText);
}

HTML: HTML:

<!-- Scripts -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/js/scripts.js"></script>

Errors I get: "Uncaught TypeError: $.ajax is not a function" . 我得到的错误: "Uncaught TypeError: $.ajax is not a function" I never get the request on my server. 我从未在服务器上收到请求。

EDIT: Problem was to do with having a jQuery slim script on another page. 编辑:问题是与另一页上的jQuery苗条脚本有关。 Removing that fixed the error. 删除该错误已修复。

You are using slim version of jQuery. 您正在使用jQuery的苗条版本。 It Doesn't support ajax Calling. 它不支持ajax调用。 Use following 使用以下

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

Update Check out following JSfiddle 更新 出以下JSfiddle

As @Santosh said make sure you add the following code in your head: 正如@Santosh所说,请确保在您的头中添加以下代码:

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

Also I found out there is a few errors in your code, I tried to find out what you want to achieve and make a code out from it, you should look though is this what you want 另外我发现您的代码中有一些错误,我试图找出您要实现的目标并从中创建代码,您应该看看这是您想要的

 $(document).ready(function() { $(".portalControls").find(".syncButton").click(triggerSync); }); function triggerSync(e) { e.preventDefault(); $.ajax({ type: "GET", url: "sync", dataType: "text", success: writeToConsole("fine"), error: writeToConsole("error") }); } function writeToConsole(consoleText) { $(".portalConsole").text(consoleText); } 
 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> <div class="portalControls"> <button class="syncButton"> click </button> </div> <div class="portalConsole"> </div> 

For the issue I found out first there is one more } here 对于我首先发现的问题,这里还有一个}

    success: writeToConsole,
    error: writeToConsole
}
^
});

Second, I don't know why you use $.find("portalConsole").text(consoleText); 其次,我不知道您为什么使用$.find("portalConsole").text(consoleText); , if you want to change the text, I'll use $(".portalConsole").text("text here...") also notice there isn't a . ,如果要更改文本,我将使用$(".portalConsole").text("text here...")还要注意,这里没有. or a # before portalConsol. 或在portalConsol之前输入#

只需添加上面的CDN脚本标签

<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>

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

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