简体   繁体   English

如何从服务器端的jQuery Ajax检索“数据”?

[英]How can I retrieve “data” from jQuery Ajax on server side?

How can I retrieve the 'data' which is sent from a client? 如何检索从客户端发送的“数据”? (in field 'data') (在“数据”字段中)

<script type="text/javascript">
    $(function () {
        $('#btnAddProductAjax').click(function () {
            var name = $('#txtProductName').val();
            var units = $('#txtUnitsInStock').val();
            var price = $('#txtPrice').val();

            $.ajax({
                url: '@Url.Action("AddProductAjax", "Home")',
                type: 'POST',
                dataType: 'JSON',
                data: {
                    productname: name,
                    unitsinstock: units,
                    price: price
                },
                success: function (data) {                        
                    $('#divResult').html(data);
                    alert('Product added successfully');
                }
            });
        });
    });
</script>

How can I use this data 我如何使用这些数据

data: {
    productname: name,
    unitsinstock: units,
    price: price
 },

in my server-side action 'AddProductAjax' ? 在我的服务器端操作'AddProductAjax'中?

public JsonResult AddProductAjax(string data)
{
    //retrieve data which is sent from client and do something
    return Json(json_data);
}

I tried : 我试过了 :

  • to get data from Request.QueryString[] 从Request.QueryString []获取数据
  • AddProductAjax(string name, int units, int price) AddProductAjax(字符串名称,整数单位,整数价格)
  • AddProductAjax(Product prod) AddProductAjax(产品生产)

Googled for hours, no result 搜索了几个小时,没有结果

UPD: If I define action like UPD:如果我定义类似

    AddProductAjax(string productname, int unitsinstock, decimal price) 

- nothing happens. - 什么都没发生。 Ajax doesn't even call this action. Ajax甚至没有调用此操作。 If I try 如果我尝试

    AddProductAjax(string productname, string unitsinstock, string price)

- in debugger all the fields are empty! -在调试器中,所有字段均为空!

It helps to specify to MVC what type of request the action should expect using Attributes. 它有助于使用MAttributes向MVC指定操作应期望的请求类型。 Both the HttpPost and HttpGet attributes are found in System.Web.Mvc . HttpPostHttpGet属性都可以在System.Web.Mvc中找到。

Like this: 像这样:

[HttpPost]
public JsonResult AddProductAjax(string productname, int unitsinstock, 
                                 decimal price)
{
    //logic...

    return Json(json_data);
}

If it doesn't work at first, there is a chance you'll need to define the parameters (the variables in the parentheses) as string s and then parse each one to retrieve the values inside, like so: 如果一开始不起作用,则可能需要将参数(括号中的变量)定义为string s,然后解析每个参数以检索其中的值,如下所示:

[HttpPost]
public JsonResult AddProductAjax(string productname, string unitsinstock, 
                                 string price)
{
    int units = 0;
    int.TryParse(unitsinstock, out units);

    decimal decPrice = 0.00;
    decimal.TryParse(price, out decPrice);

    //logic...

    return Json(json_data);
}

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

相关问题 如何使用 ajax 调用将 jQuery 中网格中已删除的行名传递给控制器(服务器端) - How do I pass the deleted row name from a grid in jQuery to controller(server-side) with ajax call 我可以从服务器端的承载令牌中检索userinfo - web api 2吗? - Can I retrieve userinfo from bearer token on server side — web api 2? 如果数据来自单独的ajax调用,如何为jqGrid添加服务器端分页?[Trirand JQgrid 4.6] - How do i add server side paging for jqGrid if data comes from a separate ajax call?[Trirand JQgrid 4.6] 在服务器端分页上防止来自jquery数据表的多个ajax调用 - Prevent multiple ajax calls from jquery datatable on server side pagination Mvc从服务器端取消Jquery Ajax请求 - Mvc cancel Jquery Ajax request from server side 如何在服务器端的模态引导程序中进行验证? - How can I validate in a modal bootstrap from server side? 如何从多个表的存储过程中检索数据? - How I can retrieve data from stored procedure in multiple table? 如何从接口的DisplayAttribute检索数据? - How can I retrieve data from DisplayAttribute of interface? 如何在AJAX发布中使用服务器端验证? - How do I use server side validation with an AJAX post? 如何获得jQuery验证以生成与服务器端ASP .NET MVC验证相同的标记? - How can I get jQuery validation to produce the same markup as server-side ASP .NET MVC validation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM