简体   繁体   English

为什么jQuery Ajax在Post之前发送Get请求?

[英]Why jQuery Ajax send Get request before of Post?

I want to pass my combobox value on button click event in ASP.Net Mvc but it send get request first then send post request. 我想在ASP.Net Mvc中的按钮单击事件上传递我的组合框值,但它先发送获取请求,然后发送发布请求。 But I don't want to send Get request. 但是我不想发送Get请求。

<script type="text/javascript">
    $(function () {
        $("#btnSave").click(function () {
            $.ajax({
                url: '@Url.Action("ExportButton")',
                type: 'POST',
                data: {
                    Parameter: mycombobox.GetValue()
                }
            });
            $('#btnSave').unbind('click');
            return false;
        });
    });
</script>

Any idea? 任何想法?

Edit: 编辑:

Here is my button: 这是我的按钮:

@Html.DevExpress().Button(itemSettings =>
{
    itemSettings.Width = 150;
    itemSettings.Styles.Native = true;

    itemSettings.Name = "btnSave";
    itemSettings.Text = "Save";
    itemSettings.UseSubmitBehavior = false ;
    itemSettings.RouteValues = new { Controller = "Report", Action = "ExportButton"};
}
).GetHtml()

Combobox: 组合框:

@Html.DevExpress().ComboBox( itemSettings =>
{

    var properties = itemSettings.Properties;
    properties.Caption = "Save As ";
    properties.Items.Add("PDF", "PDF");
    properties.Items.Add("EXCEL", "EXCEL");
    properties.Items.Add("CSV", "CSV");
    itemSettings.Name = "mycombobox";

    properties.ValueType = typeof(string);
}
).GetHtml()

Edit 2: 编辑2:

I realised that my jquery send "get" method before "Post" method. 我意识到我的jQuery在“ Post”方法之前先发送“ get”方法。

在此处输入图片说明

Thanks in advance. 提前致谢。

did you had tried this way? 您是否尝试过这种方式?

<script type="text/javascript">
    $(function () {
        $("#btnSave").unbind().click(function () {//unbind your click event here
            $.ajax({
                url: '@Url.Action("ExportButton")',
                type: 'POST',
                data: {
                    Parameter: mycombobox.GetValue()
                }
            });
        });
    });
</script>

The docs state that click is a shortcut method for .on("click", handler) and suggests using .off("click") for unbinding, instead of unbind() . 文档指出click是.on("click", handler)的快捷方法,并建议使用.off("click")进行绑定,而不是unbind() Give it a try ! 试试看 !

I don't think your Ajax is running twice. 我不认为您的Ajax运行两次。 Based on the image you've included, these requests are coming from different sources. 根据您所包含的图像,这些请求来自不同的来源。 The first one is a GET from the document and the second is the Ajax call by POST where you can see it is using XHR. 第一个是文档中的GET,第二个是POST的Ajax调用,您可以在其中看到它正在使用XHR。 Try to hover over or click on each line in order to get more detailed information. 尝试将鼠标悬停或单击每一行以获取更多详细信息。

I finally solved the problem. 我终于解决了问题。

@Html.DevExpress().Button(itemSettings =>
{
    itemSettings.Width = 150;
    itemSettings.Styles.Native = true;

    itemSettings.Name = "btnSave";
    itemSettings.Text = "Save";
    itemSettings.UseSubmitBehavior = false ;
    itemSettings.RouteValues = new { Controller = "Report", Action = "ExportButton"}; // This line send Get request!
}
).GetHtml()

When I deleted itemSettings.RouteValues line, problem solved. 当我删除itemSettings.RouteValues行时,问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM