开发者

jQuery ajax call: how to pass object without .serialize() or.param()?

I have a javascript hash (object?) we will call settings_hash that basically looks like this:

{ setting_1=90, setting_2=30, setting_3=19 }

And I've got a post that looks like this:

jQuery.getJSON("model/queries.cfc", {
    method: 'methodName',
    data: jQuery.param(settings_hash),
    }, function(data){
        // callback stuff goes here
    }
);

I really don't like having to decode the data string on my queries page (it's coldfusion- bleah). Is there an easy way to handle the decode a little better on the client side? So that what we开发者_C百科 functionally get is this:

jQuery.getJSON("model/queries.cfc", {
    method: 'methodName',
    setting1: 90,
    setting2: 30,
    setting2: 19,
    }, function(data){
        // callback stuff goes here
    }
);

Obviously if .serialize() or .param() is the way to go, then that's fine. What I want to avoid is a big long string that I have to decode, like data=setting_1%3D90%26setting2%3D30%26setting3%3D19. Open to all solutions/feedback- if the big long string is really the way to go for some reason, convince me and you'll get credit for the answer!


Your "hash" isn't a valid JavaScript object literal that can be serialized into JSON.

Convert from what you have to simply using an actualy JS object literal, like this:

var data = {
    method: 'methodName',
    setting1: 90,
    setting2: 30,
    setting2: 19
}

Then just pass to your AJAX call, no manual serialization necessary:

jQuery.post(url, data, callback);

function callback(d) {
    //stuff
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜