开发者

Jquery Post multidimensional array via $.Ajax

I am trying to post an ajax call as if it were the following form element:

<input type="text" name="data[BlogPost][title]" />

But I'm not having any luck here is my source:

    $.ajax({
        url: "/add/",
        type: "POST",
        data: ( /* what do I do here */),
        success: function(msg){
            alert(msg);
        }
    });

I've tried nested objects but that only generates a server response like: array 'data' => string '[obje开发者_如何学JAVAct Object]' (length=15)

Which does nobody any good!

Any thoughts?


Just put the field name in quotes, also notice I am using an object literal for the data parameter {} vs the parens you had in your question:

$.ajax({
    url: "/add/",
    type: "POST",
    data: { 'data[BlogPost][title]':'My New Title'} ,
    success: function(msg){
        alert(msg);
    }
});


Have you tried serialize()?

$.ajax({
    url: "/add/",
    type: "POST",
    data: $('#myForm').serialize(),
    success: function(msg){
        alert(msg);
    }
});

I'm not 100% sure it works on multidimensional arrays but it's worth a shot.


My guess, [..] square brackets are not valid characters for names of input elements?

Correct me if I'm wrong.

Update: Oops, ok so I'm wrong. Will leave this here anyway as a 'learning' info. For others like me :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜