weixin_33709364 2016-06-16 19:20 采纳率: 0%
浏览 53

软代码Jquery Ajax数据密钥

I am new to Javascript and Jquery and I'm trying to make a function for a flexible api call. What I'm wondering is: is there a way to soft code ajax Data keys?

For example:

var call = $.ajax({
    url: url,
    data: {Param: paramValue},
    success: successFunction,
    error: errorFunction
  });

where Param is not an actual parameter in the api call like url/api?Param=paramvalue, rather a variable I had defined earlier like Param = userInput(). So that if the user enters 'metrics' , I will call url/api?metrics.

If not, is there another way of doing this that I should be made aware of?

Thanks for your thoughts!

  • 写回答

1条回答 默认 最新

  • weixin_33716154 2016-06-16 19:29
    关注

    Well, you could use this way to define the data object :

    var param = "metrics";
    var data = {};
    data[param] = "";
    var call = $.ajax({
        url: url,
        data: data,
        success: successFunction,
        error: errorFunction
    });
    
    评论

报告相同问题?