Jquery$.post方法

Jquery$.post方法,jquery,asp.net-mvc-2,Jquery,Asp.net Mvc 2,嗨,我有一个这样的方法: [AcceptVerbs(HttpVerbs.Post)] public JsonResult GetPayeesJson(long id) { ///TODO: } [AcceptVerbs(HttpVerbs.Post)] public JsonResult GetPayeesJson(long id, string formName) {

嗨,我有一个这样的方法:

 [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetPayeesJson(long id)
        {
             ///TODO:
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetPayeesJson(long id, string formName)
        {
           //TODO:
        }
现在,它是从如下Javascript函数调用的,运行良好:

$.post("/Payee/GetPayeesJson/" + payerData.Id, null, function (data) {
               fillPayeeCache(data, payerData.Id);
               fillPayeeSelect(payeeCache[payerData.Id]);
            }, "json");
除了传递给方法
getPayesJSON(长id)
的id之外,我还想传递一个包含表单名称的字符串。基本上我想对这样的方法进行post调用:

 [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetPayeesJson(long id)
        {
             ///TODO:
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetPayeesJson(long id, string formName)
        {
           //TODO:
        }

我怎么能做到这一点。提前谢谢

在$.post调用中将formName作为查询字符串变量添加到url中可能是最简单的方法。(即在url中添加“?formName=任何内容”)您也可以将其作为名为formName的表单变量发布

此行为是可重写的,但默认情况下,您可以使用适当命名的querystring或form变量

e:offtopic,但您也应该能够在getPayesJSON方法上使用[HttpPost]作为属性的缩写形式

e2:更多的特异性。。。 添加为查询字符串->
“/Payee/GetPayesJSON/“+payerData.Id+”?formName=WhateverYourFormsName”

添加为表单变量->参见Toast的回复


这两个选项都允许您使用指定的控制器操作语法。

示例:请求test.php页面并发送一些附加数据(同时仍然忽略返回结果)


来源:

这个怎么样

$.ajax({
    url: 'example.php',
    dataType: 'json',
    type: 'POST',
    data:  { 
        formName: 'formName' 
    },
    success: function (response) {

    },
    error: function(response, data) {
      alert("Oops... Looks like we're having some difficulties."); 
   }         
});

您需要将该信息作为提交的
数据的一部分传递

那么你的具体例子呢

$.post("/Payee/GetPayeesJson/" + payerData.Id, 
       {'formName':'value to be passed here'}, 
       function (data) {
               fillPayeeCache(data, payerData.Id);
               fillPayeeSelect(payeeCache[payerData.Id]);
            }, "json");

应该做你需要的事。

谢谢你的回答。你能说得更具体些吗。我正在使用asp.net MVC framework,此调用正在传递给一个控制器类。这默认为“post”,但我认为在此处使用
类型:'post'
将其显式化会很有用,因为OP的操作方法需要它。