[英]Can't make jQuery and Ajax form submit work
我真的是Web编程的新手。 我正在尝试进行表单发布并获取回调。
我正在尝试在这里使用此插件: 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。 浏览器将通过将那些相对URL附加到当前目录的末尾来从您的站点请求资源。
假设此页面位于http://localhost/myapp/mypage.html
。 浏览器将在以下位置查找您的脚本文件:
http://localhost/myapp/ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js http://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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.