如果我在ajax调用中使用两个变量,则获取null值

时间:2016-05-11 11:17:43

标签: javascript ajax asp.net-mvc asp.net-ajax

我在ajax调用中调用Action Method但是当我在data:test中使用单个varibale时,值即将到来,但是当我使用data: '{"testData":"' + test + '","feature":"' + test + '"}'时,它为这两个变量赋予空值。 以下是javascript的代码

$.ajax({
        url: '@(Url.Action("UpdatePlanFeatVal", "SuperAdmin"))',
        type: "POST",
        dataType: "json",
        //data: JSON.stringify({ testData: test, feature: test}),
        // data: '{"testData":"' + test + '","feature":"' + test + '"}',
        data:test,
        success: function (result) {  
        },
        error: function (err) { 
        }
    });

和动作控制器

public ActionResult UpdatePlanFeatVal(string testData, string feature)
{   
   var cmd = (object)null;
   testData = testData.Trim();
   feature = feature.Trim();
   using (StoreProcedureContext sc = new StoreProcedureContext())
    {            
        cmd = sc.EditPricing(testData, feature);         

    }
   return View("ManageSubscriptionPlan");
}

请告诉我我在哪里做错了,并且它也没有重定向到ManageSubscriptionView。

2 个答案:

答案 0 :(得分:1)

Below code will help you to send information in object that value will able to receive to controller side.

    var obj = {
        "testData": "test",
        "feature": "test"
    };
    $.ajax({
        type: "POST",
        url: '@(Url.Action("UpdatePlanFeatVal", "SuperAdmin"))',
        data: JSON.stringify(obj),
        dataType: "json",
        contentType: "application/json",
        success: function (data) { alert(data.chardata); },
        error: function (a, b, c) {
            alert("Error");
        }
    });

答案 1 :(得分:0)

试试这个:

$.ajax({
    type: "POST",
    url: '@Url.Action("UpdatePlanFeatVal", "SuperAdmin")',
    data: { testData: 'test', feature: 'test' },
    dataType: "json",
    success: function (result) {  
    },
    error: function (err) { 
    }
});