无法使 jQuery 和 Ajax 表单提交工作


Can't make jQuery and Ajax form submit work

我对网络编程真的很陌生。我正在尝试发布表单并收到回调。

我正在尝试在这里使用此插件:http://malsup.com/jquery/form/#ajaxSubmit

但是当我打电话给:$("#my_form").ajaxSubmit(options);什么也没发生..

到目前为止我做了什么:

我有这个表格

  <form method="post" id="my_form" action="record.php"  enctype="multipart/form-data" >
    // stuff inside..
    <input type="button" id = "recordDatabase" value="Rcord on Database" />
  </form>

我有这个脚本:

 <script src="http://malsup.github.com/jquery.form.js"></script>
 $(document).ready(function()
 {
      var options = 
      {
         beforeSubmit:  showRequest,  // pre-submit callback
         success:       showResponse  // post-submit callback
      };
      $("#recordDatabase").click(function()
      {
         alert('About to submit: 'n'n');
         $("#my_form").ajaxSubmit();
         alert('submited: 'n'n');
         return false;
       });
 });

最后我的两个函数是:

function showRequest(formData, jqForm, options) 
    {
            // formData is an array; here we use $.param to convert it to a string to display it
            var queryString = $.param(formData);
            alert('About to submit: 'n'n' + queryString);
            return true;
    }
function showResponse(responseText, statusText, xhr, $form)
    {
        alert('status: ' + statusText + ''n'nresponseText: 'n' + responseText +
            ''n'nThe output div should have already been updated with the responseText.');
    }

我的做法与网站上的示例(http://malsup.com/jquery/form/#ajaxSubmit)完全相同,但它不起作用。

知道出了什么问题吗?

我认为你不能在Git上热链接到jQuery插件。尝试下载插件并将其另存为 JS 文件保存在应用程序 Web 根目录中。

看起来您没有正确引用脚本。 根据您的评论,您已经包含了这样的脚本:

<script src="ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="localhost/js/jqueryPlugin.js"></script>

这些是相对网址。 浏览器将通过将这些相对 URL 附加到当前目录的末尾来请求来自您的网站的资源。

假设此页面位于 http://localhost/myapp/mypage.html 。 浏览器将在以下位置查找您的脚本文件:

http://localhost/myapp/ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.jshttp://localhost/myapp/localhost/js/jqueryPlugin.js

这些网址可能不存在。 也许您的浏览器足够智能,可以将"ajax.googleapis.com"识别为域名并从域请求数据,但它不太可能将"localhost"识别为域。

//添加到 URL 的开头。 这是一个"架构相对 URL",将根据当前页面使用的内容使用 http 或 https。 我们使用此类 URL 来避免安全提示警告用户页面上的某些内容不安全。 然后,您的脚本将如下所示:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//localhost/js/jqueryPlugin.js"></script>