我正在尝试对“ http://www.omdbapi.com/?i=tt2975590&apikey=”进行API调用,我想知道是否有人可以向我展示如何通过javascript代码传递i参数。目前为i = tt2975590。
let detailMovie= "http://www.omdbapi.com/?i=tt2975590&apikey=<mykey>
let request = new XMLHttpRequest()
request.open('Get',movieURL)
request.send()
request.onload = function() {
if(request.status !=200){
console.log("There is a problem")
} else {
let moviesResponse = JSON.parse(request.responseText)
I want to be able to call http://www.omdbapi.com/?i=tt2975590&apikey=<mykey> but pass in the paramater(i) value.
}
}
在此处输入代码
答案 0 :(得分:0)
使用 ${}
let key = "1234";
let apikey = "878234";
let detailMovie = `http://www.omdbapi.com/?i=${key}&apikey=${apikey}`;
console.log(detailMovie);
或使用返回api的函数
function genApi(key, apikey) {
return `http://www.omdbapi.com/?i=${key}&apikey=${apikey}`;
}
函数示例可以像您这样花费很多时间
const myApi = genApi("123", "23423");
// will be "http://www.omdbapi.com/?i=123&apikey=23423"
答案 1 :(得分:0)
您可以使用axios简化api的调用。 这里是如何将参数传递给get请求的代码段。
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
// Optionally the request above could also be done as
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
// always executed
});
答案 2 :(得分:0)
将i的值存储在某些变量中,例如:
var x ='tt2975590';
然后在您的网址中使用它,
let detailMovie =“” http://www.omdbapi.com/?i=“ + z +”&apikey =“
使用上面的detailMovie变量打开您的请求。
答案 3 :(得分:0)
很少有浏览器弃用XMLHttpRequest()
,因此最好并强烈建议使用fetch API。 https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
您可以使用URLSearchParams(params)
通过url传递值。
例如
var url = new URL('https://sl.se')
var params = {lat:35.696233, long:139.570431} // or:
var params = [['lat', '35.696233'], ['long', '139.570431']]
url.search = new URLSearchParams(params)
fetch(url)
.then(data => data.text())
.then((text) => {
console.log('request succeeded with JSON response', text)
}).catch(function (error) {
console.log('request failed', error)
})
答案 4 :(得分:0)
HttpUrl url1 = HttpUrl.parse("https://www.google.com").newBuilder() .addQueryParameter("search","parameter") .build();