jQuery AJAX POST变量无法在服务器上访问

时间:2010-07-15 00:20:30

标签: jquery asp.net-mvc-2 client-side

我正在使用jQuery将一些数据发布到asp.net mvc网站。当我在Visual Studio中调试它时,帖子成功发生并且目标控制器操作被命中,但是我传递的数据似乎没有被转移。我无法在Request.Params或Request.Headers中看到它我的帖子如下:

$.ajax({
      url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
      headers: {signature: authHeader},
      type: "POST",
      // tried this data: authHeader
      success: function() { alert('Success!'); }
    });

我在这里错过了什么吗?

2 个答案:

答案 0 :(得分:4)

您需要在data属性中发布您的数据:

$.ajax({
  url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
  data: {signature: authHeader},
  type: "POST",
  // tried this data: authHeader
  success: function() { alert('Success!'); }
});

答案 1 :(得分:2)

headers不是$.ajax()的有效选项,您需要data,如下所示:

$.ajax({
  url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
  data: {signature: authHeader},
  type: "POST",
  success: function() { alert('Success!'); }
});

如果你想要设置标题,你应该在beforeSend事件处理程序中执行此操作,如下所示:

$.ajax({
  url: "http://localhost/PlatformPortal/Buyers/Account/SignIn",
  beforeSend: function(xhr) {
     xhr.setRequestHeader("signature", authHeader);
  },
  type: "POST",
  success: function() { alert('Success!'); }
});