且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

执行其余URL后未调用jQuery成功函数

更新时间:2022-12-16 16:58:18

查看 https://api.jquery .com/trigger ,我认为您不能将函数作为第二个参数传递给"trigger"方法.该参数应该是包含一些额外选项的对象或数组,然后再将这些选项传递给事件处理程序函数(应在其他地方声明).您在此处定义的匿名函数将运行.因此,该表单的正常回发行为将继续进行,并且不会进行ajax调用.这就是为什么您似乎看到一个响应,但没有触发成功"或错误"回调的原因-首先从来没有ajax调用.

Looking at https://api.jquery.com/trigger, I don't think you can pass a function as the second parameter to the "trigger" method. That argument should be an object or array containing some extra options which are then passed to the event handler function (which should be declared elsewhere). The anonymous function you've defined there runs. Therefore, the normal postback behaviour of the form carries on regardless, and there's no ajax call. That's why you appear to see a response, but don't get the "success" or "error" callbacks triggered - there's never an ajax call in the first place.

您所做的不是在元素上定义事件的正确方法. 触发"方法旨在触发先前已定义的事件.它不能用于定义事件处理程序本身.

What you've done is not the correct way to define an event on an element. The "trigger" method is intended to trigger an event which has already been defined previously. It can't be used to define the event handler itself.

这应该工作-它为表单的"submit"事件创建一个事件处理程序,该事件处理程序将取消默认的回发行为并改为运行ajax调用:

This should work - it creates an event handler for the "submit" event of the form, which suppresses the default postback behaviour and runs the ajax call instead:

$(document).ready(function() {
  $("#CreateAttachmentForm").submit(function(event) {
    event.preventDefault(); //stop default postback behaviour.
    $.ajax({
      url: 'http://HDDT0214:8080/pqawdTestWebApp/uploadFile',
      type: 'POST',
      data: formData,
      success: function (data) {
        alert("test");
        alert(JSON.stringify(data));
      },
      error: function(jqXHR, errorThrown, textStatus) {
        alert("Ajax error: " + jqXHR.status + " - " + jqXHR.statusText);
        console.log(jqXHR.responseText);
      },
      contentType: false,
      processData: false
    });
  });
});

然后您可以删除createAttachmentRequest()函数及其所有引用.

You can then remove the createAttachmentRequest() function and all references to it.

只要任何<contact:contactbutton都在表单内向页面呈现<input type="submit"...<button type="submit"...样式按钮,此方法就起作用.如果不是,则需要对此进行修改以输出正确的元素,否则它将不会触发表单的本机提交事件.

N.B. This will work as long as whatever <contact:contactbutton is renders an <input type="submit"... or <button type="submit"... style button to the page, within the form. If not, you need to amend this to output the correct element, otherwise it will not trigger the form's native submit event.