以ajax jquery形式传递querystring参数
我正在尝试将一些参数传递给 asp.net mvc 2 应用程序。我正在使用 jqueryform 插件。我有 3 个链接,每个链接在上传文件时传递不同的类型。我正在使用一个隐藏字段,稍后从服务器上的查询字符串中读取该字段。我已经尝试过,但请求未发布?
<script type="text/javascript">
$(document).ready(function () {
function subm() {
$('#fileUploadForm').ajaxForm({
url: "/Home/Upload/",
method: "POST",
beforeSubmit: ShowRequest,
success: SubmitSuccesful,
error: AjaxError
});
}
function ShowRequest(formData, jqForm, options) {
var queryString = $.param(formData);
alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
return true;
}
function AjaxError() {
alert("An AJAX error occured.");
}
function SubmitSuccesful(responseText, statusText) {
alert("SuccesMethod:\n\n" + responseText);
}
$("#uploadLink").click(function () {
// set type
$("input[name=hiddenField]").val("Type1");
subm();
})
});
</script>
<body>
<form id="fileUploadForm" action="" enctype="multipart/form-data">
<input type="text" name="filename" />
<input type="file" id="postedFile" name="postedFile" />
<input type="hidden" name="hiddenField" />
<a id="uploadLink">upload type 1</a> <a id="uploadLink2">upload type 2</a> <a id="uploadLink3">
upload type 3</a>
</form>
</body>
public FileUploadJsonResult Upload(HttpPostedFileBase postedFile)
{
var type = Request.QueryString["hiddenField"];
return new FileUploadJsonResult { Data = new { message = "success" } };
}
public class FileUploadJsonResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
this.ContentType = "text/html";
context.HttpContext.Response.Write("<textarea>");
base.ExecuteResult(context);
context.HttpContext.Response.Write("</textarea>");
}
}
I am trying to pass some parameters to an asp.net mvc 2 application. I am using the jqueryform plugin. I have 3 links that each pass a different type when uploading the file. I am using a hiddenfield that is later read from the queystring on the server. I have tried this but the request does not get posted?
<script type="text/javascript">
$(document).ready(function () {
function subm() {
$('#fileUploadForm').ajaxForm({
url: "/Home/Upload/",
method: "POST",
beforeSubmit: ShowRequest,
success: SubmitSuccesful,
error: AjaxError
});
}
function ShowRequest(formData, jqForm, options) {
var queryString = $.param(formData);
alert('BeforeSend method: \n\nAbout to submit: \n\n' + queryString);
return true;
}
function AjaxError() {
alert("An AJAX error occured.");
}
function SubmitSuccesful(responseText, statusText) {
alert("SuccesMethod:\n\n" + responseText);
}
$("#uploadLink").click(function () {
// set type
$("input[name=hiddenField]").val("Type1");
subm();
})
});
</script>
<body>
<form id="fileUploadForm" action="" enctype="multipart/form-data">
<input type="text" name="filename" />
<input type="file" id="postedFile" name="postedFile" />
<input type="hidden" name="hiddenField" />
<a id="uploadLink">upload type 1</a> <a id="uploadLink2">upload type 2</a> <a id="uploadLink3">
upload type 3</a>
</form>
</body>
public FileUploadJsonResult Upload(HttpPostedFileBase postedFile)
{
var type = Request.QueryString["hiddenField"];
return new FileUploadJsonResult { Data = new { message = "success" } };
}
public class FileUploadJsonResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
this.ContentType = "text/html";
context.HttpContext.Response.Write("<textarea>");
base.ExecuteResult(context);
context.HttpContext.Response.Write("</textarea>");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ajaxForm
不发送 AJAX 请求。它 AJAX 化了一个表单。如果你想强制 AJAX 提交,你应该使用ajaxSubmit
:另外为什么你要硬编码 AJAX 请求的 url 和方法。在构建表单时执行此操作:
然后简单地:
这将确保您的应用程序无论托管在何处都可以正常工作。因为如果您像以前那样对 url 进行硬编码,并且部署在 IIS 中的虚拟目录中,则 url 将不再有效。
ajaxForm
doesn't send an AJAX request. It AJAXifies a form. If you want to force the AJAX submission you should useajaxSubmit
:Also why are you hardcoding the url and method of the AJAX request. Do this when constructing your form:
and then simply:
This will ensure that your application will work no matter where it is hosted. Because if you hardcode the url as you did and if you deploy in a virtual directory in IIS the url will no longer be valid.