以ajax jquery形式传递querystring参数

发布于 2024-11-18 05:38:32 字数 2143 浏览 6 评论 0原文

我正在尝试将一些参数传递给 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-11-25 05:38:32

ajaxForm 不发送 AJAX 请求。它 AJAX 化了一个表单。如果你想强制 AJAX 提交,你应该使用 ajaxSubmit

$(function() {
    // AJAXify the form
    $('#fileUploadForm').ajaxForm({
        url: '/Home/Upload/',
        method: 'POST',
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
     });
});

// the next functions could be declared outside of the document.ready handler

function subm() {
    $('#fileUploadForm').ajaxSubmit();
}

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();
    return false;
});

另外为什么你要硬编码 AJAX 请求的 url 和方法。在构建表单时执行此操作:

<% using (Html.BeginForm("Update", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "fileUploadForm" })) { %>
    <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>
<% } %>

然后简单地:

$(function() {
    $('#fileUploadForm').ajaxForm({
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
     });
});

这将确保您的应用程序无论托管在何处都可以正常工作。因为如果您像以前那样对 url 进行硬编码,并且部署在 IIS 中的虚拟目录中,则 url 将不再有效。

ajaxForm doesn't send an AJAX request. It AJAXifies a form. If you want to force the AJAX submission you should use ajaxSubmit:

$(function() {
    // AJAXify the form
    $('#fileUploadForm').ajaxForm({
        url: '/Home/Upload/',
        method: 'POST',
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
     });
});

// the next functions could be declared outside of the document.ready handler

function subm() {
    $('#fileUploadForm').ajaxSubmit();
}

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();
    return false;
});

Also why are you hardcoding the url and method of the AJAX request. Do this when constructing your form:

<% using (Html.BeginForm("Update", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "fileUploadForm" })) { %>
    <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>
<% } %>

and then simply:

$(function() {
    $('#fileUploadForm').ajaxForm({
        beforeSubmit: ShowRequest,
        success: SubmitSuccesful,
        error: AjaxError
     });
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文