通过$动态发送数据。 ajax方法

时间:2013-01-14 10:44:00

标签: c# jquery ajax asp.net-mvc

我在MVC网站项目中使用jquery。

我有一个名为'testController'的简单C#控制器:

public class testController
{
    public string test(string id)
    {
        return id;
    }
}

..以及使用它的javascript函数:

function myfunct(sData) {
    $.ajax({
        type: "POST",
        async: false,
        datatype: "json",
        url: '/test/test',
        data: '{' + sData + '}', //Not Working if I set sData = "id:'Hello'"
                                 //Work if I write data = { id:'hello'},
        success: function (json) {
            alert(json); //Need to return 'Hello'
        }
    });
}

..有一种方法可以动态分配“数据”参数吗?

6 个答案:

答案 0 :(得分:4)

只要您传递的对象中的属性与操作接受的类型的属性匹配,您就可以根据需要创建对象。试试这个:

var sData = { id: 1234 }; // note 'id' matches the property in the signature of the action method

// in the $.ajax() call properties...
data: sData

这也适用于班级。例如,如果您的控制器中有此类和操作:

public class Foo {
    public int Id { get; set; }
    public string Name { get; set; }
}

// in controller:
public ActionResult Bar(Foo myFoo) {
    // do stuff with Foo
}

然后,您可以通过$.ajax以这种形式传递javascript对象:

var data = { Id: 1234, Name: 'Foo bar' };

请注意,属性将映射到方法签名中的类的属性。

答案 1 :(得分:2)

如果key及其value都是动态的,则您需要使用稍微不同的语法来创建在调用时设置为data属性的对象jQuery.ajax()

一般原则是:

var key = 'id', value = 'Hello', data = {};
data[key] = value;

这是因为在JavaScript中,对象键(或属性)不必是字符串。以下是完全有效和等效的:

var obj = {id : 'Hello'};
var obj = {'id' : 'Hello'};

因此,如果您尝试使用存储在变量中的动态值,并在尝试创建它时将其传递给对象文字,则JavaScript 不知道它应该使用具有该名称的变量的值,而只是将其视为对象中的键。

在您的情况下,您可能想要更改函数,以便将对象本身传递给它,如下所示:

function myfunct(sData) {
    $.ajax({
        type: "POST",
        async: false,
        datatype: "json",
        url: '/test/test',
        data: sData,
        success: function (json) {
            alert(json); //Need to return 'Hello'
        }
    });
}

然后像这样使用它:

var key = 'id', value = 'Hello', data = {};
data[key] = value;
myfunct(data);

至于为什么data: '{' + sData + '}'不起作用,当你传递一个字符串作为data属性的值时,jQuery只是将它用作AJAX请求的查询字符串,而字符串是你'传递不会设置正确的参数。

答案 2 :(得分:0)

您可以创建一个单独的变量,将参数发送到控制器中的操作。

function myfunct(sData) {
    $.ajax({
        type: "POST",
        async: false,
        datatype: "json",
        url: '/test/test',
        data: { id: 'hello'}, // pass directly your parameter in a dynamic object
        success: function (json) {
            // your can read: json.Id , json.Name, etc...
            alert(json.id); //Need to return 'Hello' in ID property
        }
    });
}

在控制器上,您可以获取字符串并返回JSon格式结果。

public class TestController
{
    public ActionResult Test(string id)
    {
        string valueResult = id; // process your result
        return Json(new { id: valueResult, name = 'User' }, JsonRequestBehavior.AllowGet.);
    }
}

答案 3 :(得分:0)

尝试以下

var mydata = { key : value };

    $.ajax({
        type: "POST",
        async: false,
        datatype: "json",
        url: '/test/test',
        data: mydata,
        success: function (json) {
            alert(json); //Need to return 'Hello'
        }
    });

答案 4 :(得分:0)

可能你正在寻找这个:

在您的情况下,

jQuery.parseJSON()会有所帮助:http://api.jquery.com/jQuery.parseJSON/

function myfunct(sData) {

    $.ajax({
        type: "POST",
        async: false,
        datatype: "json",
        url: '/test/test',
        data: jQuery.parseJSON(sdata),
        success: function (json) {
            alert(json); //Need to return 'Hello'
        }
    });
}

答案 5 :(得分:0)

我找到了解决方案:

正如@ Anthony Grist在之前的回答中提到的,我需要使用一个对象,而不是一个字符串...所以使用'。 eval()'方法我即时创建我的键值对。

这样的事情:

function myfunct(sData) {
eval('var ssData = {' + sData + '}');
$.ajax({
    type: "POST",
    async: false,
    datatype: "json",
    url: '/test/test',
    data: ssData,
    success: function (json) {
        alert(json); 
    }
});
}
myfunct("id:'hello'"); //Return Hello
myfunct("id:'bye'"); //Return bye

......这似乎对我有用......你怎么看?