参数未在AJAX调用中传递

时间:2013-06-19 06:45:20

标签: c# asp.net-mvc jquery

我正在使用以下代码

function test()
{
   GetAttributesForSelectedControlType('Phone Number');
}

function GetAttributesForSelectedControlType(questionType) {
    alert(questionType);
    $.ajax({
        url: '/Wizards/GetAttributesForSelectedControlType/' + questionType,
        type: "GET",
        contentType: "application/json",
        success: function (result) {
            alert('success');
        }

    });
}

请注意:QUESTIONTYPE是STRING值,不是任何类型..

问题是在控制器中,我遇到"GetAttributesForSelectedControlType"函数,但参数值为空。 我在questionType发送字符串。 关于这个的任何想法?

3 个答案:

答案 0 :(得分:1)

function GetAttributesForSelectedControlType(questionType) {
    alert(questionType);
    $.ajax({
        url: '/Wizards/GetAttributesForSelectedControlType',
        contentType: "application/json",
        data: {
            questionType: questionType
        },
        success: function (result) {
            alert('success');
        }
    });
}

答案 1 :(得分:0)

如果您希望将问题类型作为参数传递,则应使用data:{qType:questionType}这将填充函数GetAttributesForSelectedControlType的参数qType

答案 2 :(得分:0)

尝试:

function GetAttributesForSelectedControlType(questionType) {

    $.get('/Wizards/GetAttributesForSelectedControlType', {questionType: questionType })
        .done(function(data) {
            alert('success');
    });

}

您需要传递questionType作为数据。或者,您可以将以下内容添加到现有的ajax调用中。

data: {questionType: questionType }

这将适用于以下操作:

public ActionResult GetAttributesForSelectedControlType(string questionType)
{
    // ...

}