[英]pass parameters to ajax call in javascript
Currently I'm editing a string, query, to pass to my .getJSON function. 目前我正在编辑一个字符串,查询,以传递给我的.getJSON函数。 Its starting to get messy, and I'm wondering if there is a better way to pass parameters in javascript?
它开始变得凌乱,我想知道是否有更好的方法在javascript中传递参数?
var twitter = {
query: "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=" + user_name + "&count=" + count + "&callback=?",
tweets: 3
};
$.getJSON(twitter.query,
function(json) {
console.log(json);
}
});
})
$.getJSON()
optionally accepts a data argument which will build that string for you: $.getJSON()
可选地接受一个数据参数,它将为您构建该字符串:
var url = 'https://api.twitter.com/1/statuses/user_timeline.json?callback=?';
var parameters = {
include_entities: true,
include_rts: true,
screen_name: user_name,
count: count
}
$.getJSON(url, parameters, function(json) {
console.log(json);
});
Assuming you're using jQuery.getJSON
, you can pass the parameters as an object, separate from the URL: 假设您正在使用
jQuery.getJSON
,您可以将参数作为对象传递,与URL分开:
$.getJSON('https://api.twitter.com/1/statuses/user_timeline.json', {
include_entities:true,
include_rts:true,
screen_name:user_name,
count:count
/* I excluded callback */
}, function(json) {
console.log(json);
}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.