0

I have a need to build the data string dynamically. This is not working, as it is just passing the param variable as a string.

var parameters = "{foo: 'test'}";
    $.ajax({
        url: 'test.php',
        data: parameters,
        type: 'get',
        dataType: 'json
    });

Any ideas?

4

2 回答 2

1

Well, first you assign a string to parameters variable, but then expect it to turn into object? ) Use object in the first place, like this:

var params = {foo: 'test'};
$.ajax({..., data: params, ...});
于 2012-06-14T22:52:52.687 回答
0
var parameters = {foo:'test'};
//modify `parameters` dynamicaly
parameters[bar]='dynamic!';
$.ajax({
    //...
    data: (sendJSON?JSON.stringify(parameters):parameters) // sends params either JSON or form encoded
    //...
});
于 2012-06-14T22:53:14.833 回答