【问题标题】:Getting a Value can not be null with ajax使用ajax获取值不能为空
【发布时间】:2013-01-26 23:42:26
【问题描述】:

我试图将一个参数从我的视图传递到我的控制器,我遇到了: 值不能为空,当我尝试执行 GetCompanyDataWithId 操作时。

成功运行会将参数传递给控制器​​,该控制器将返回用于填充我的 jqGrid 的 JSON 数据。


似乎没有正确传递值 有什么想法吗?

$("#dropdwnlist").change(function () {
               var value = $(this).val();

               $.ajax({
                   url: '<%= Url.Action("GetCompanyDataWithId", "Billing") %>', 
                   type: "POST",                            
                   data: { companyid: "25" },
                   success: function (data) {
                       alert(data);
                       $('#Bgrid').setGridParam({ url: '<%= Url.Action("GetCompanyDataWithId", "Billing") %>'});
                       $('#Bgrid').trigger('reloadGrid');                                                    
                   },
                   error: function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.responseText);
                   }

               }).done(function (msg) {
                   alert("Data Saved: " + msg);
               });
           });

【问题讨论】:

  • 这真的不是服务器端问题吗?
  • 呼应 adeneo,看起来您在这方面拥有更多技术,而不仅仅是 jQuery。我认为您正在使用某些版本的 Asp.NET MVC 和一些表格网格插件。如果您包含这些详细信息,它将真正帮助人们回答您的问题。至少,您可以捕获更改函数正在发送的 Ajax 请求的正文并将其发布到此处。

标签: jquery


【解决方案1】:

假设您的控制器方法如下所示:

public ActionResult GetCompanyDataWithId(string companyid) {}

您可以使用这些$.ajax 选项:

data: JSON.stringify({ companyid: "25" }),
type: 'post',
dataType: 'json',
contentType: 'application/json; charset=utf-8',

或者更简单:

Url.Action("GetCompanyDataWithId", "Billing", new { companyid = "25" })

【讨论】:

  • JSON.stringify 可能不对。控制器可能希望 POST 数据采用普通的x-www-form-urlencoded 格式,而 jQuery 会自动进行编码。
  • @Barmar,这是必需的。特别是对于复杂的对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
  • 2020-04-04
  • 2013-05-09
  • 1970-01-01
  • 2021-11-08
  • 2021-07-10
相关资源
最近更新 更多