当我尝试在jquery中调用一个动作时,mvc ajax不起作用

时间:2021-05-29 11:27:24

I want to send quantity info and product id to a controller using ajax. after I clicked addtocart button and nothing happened,page didnt refreshed. I refreshed the page manually and saw that requested quantity added to cart successfully. Why the page does not refresh automatically? Can you please help me what wrong is here?

我想使用ajax将数量信息和产品ID发送到控制器。点击addtocart按钮后没有任何反应,页面没有刷新。我手动刷新页面并看到请求的数量已成功添加到购物车。为什么页面不会自动刷新?你能帮我解决一下这里有什么问题吗?

<script>  
    $("#addtocart").click(function () {

        alert("tıklandı");
        var val1;
        var val2;
        val1 = $(".cart_quantity_input").val();
        val2 = $(".product-name-text").attr("id");
        alert("değerler alındı");
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/Home/AddToCart/",
            data: { id: val2, quantity: val1 },
            success: function (Data) {
                alert("ajak içindeyiz");
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {

            }
        });
        alert("ajak bitti");
    });

</script>

1 个解决方案

#1


0  

You're using an Ajax function to send your data. I would normally use Ajax specifically to avoid the page being refreshed. If you want to refresh the page then you should be using a form with a submit button to your controller instead.

您正在使用Ajax函数发送数据。我通常会专门使用Ajax来避免刷新页面。如果要刷新页面,则应该使用带有提交按钮的表单来代替控制器。

However, I expect the issue with your success function not being hit is because you are specifying the data type as json. This is telling the Ajax function that json data is expected both ways so your controller action should return json. As a guess, it looks more like:

但是,我希望您的成功函数未被命中的问题是因为您将数据类型指定为json。这告诉Ajax函数,json数据是双向的,所以你的控制器动作应该返回json。作为猜测,它看起来更像是:

public ActionResult AddToCart(int id, int quantity)
{
    // blah blah blah
    return View();
}

If this is the case then remove the datatype parameter from your ajax call and it should then hit the success function.

如果是这种情况,那么从ajax调用中删除datatype参数,然后它应该命中成功函数。

But you should either be returning json or a ContentResult from the action and then add logic to update the cart display based on the results. Or use a form and submit the data if you want to refresh the whole page.

但是您应该从操作中返回json或ContentResult,然后添加逻辑以根据结果更新购物车显示。或者,如果要刷新整个页面,请使用表单并提交数据。

#1


0  

You're using an Ajax function to send your data. I would normally use Ajax specifically to avoid the page being refreshed. If you want to refresh the page then you should be using a form with a submit button to your controller instead.

您正在使用Ajax函数发送数据。我通常会专门使用Ajax来避免刷新页面。如果要刷新页面,则应该使用带有提交按钮的表单来代替控制器。

However, I expect the issue with your success function not being hit is because you are specifying the data type as json. This is telling the Ajax function that json data is expected both ways so your controller action should return json. As a guess, it looks more like:

但是,我希望您的成功函数未被命中的问题是因为您将数据类型指定为json。这告诉Ajax函数,json数据是双向的,所以你的控制器动作应该返回json。作为猜测,它看起来更像是:

public ActionResult AddToCart(int id, int quantity)
{
    // blah blah blah
    return View();
}

If this is the case then remove the datatype parameter from your ajax call and it should then hit the success function.

如果是这种情况,那么从ajax调用中删除datatype参数,然后它应该命中成功函数。

But you should either be returning json or a ContentResult from the action and then add logic to update the cart display based on the results. Or use a form and submit the data if you want to refresh the whole page.

但是您应该从操作中返回json或ContentResult,然后添加逻辑以根据结果更新购物车显示。或者,如果要刷新整个页面,请使用表单并提交数据。