使用 fetch() 或 XMR/any_other_type_of_ajax-request 将特定数据放入 ajax 请求中,就像在 jQuery ajax 中一样
put specific data in ajax request using fetch() or XMR/any_other_type_of_ajax-request, like the same in jQuery ajax
我有一个简单的请求,使用 jQuery ajax()
getJson() {
$.ajax({
url: 'http://api.rss2json.com/v1/api.json',
method: 'GET',
dataType: 'json',
data: {
rss_url: 'https://news.yandex.ru/sport.rss',
api_key: 'lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf',
count: 15
}
})
// other code
}
这是对服务的请求,需要 rss(来自 rss_url)和 returns JSON。一切都很好,但现在我必须在没有 jQuery 的情况下发出相同的 ajax 请求。
此请求的所有参数都必须像这样(因为服务只理解这些参数)。
我尝试使用 fetch() 做同样的事情:
getJson() {
let url = 'http://api.rss2json.com/v1/api.json';
let config = {
method: 'GET',
dataType: 'json',
data: {
'rss_url': 'https://news.yandex.ru/politics.rss',
'api_key': 'lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf',
count: 15
}
}
fetch(url, config)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
尝试使用 XMLHttpRequest
getJson() {
let xhr = new XMLHttpRequest();
let jsonData = JSON.stringify({
rss_url: 'https://news.yandex.ru/politics.rss',
api_key: 'lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf',
count: 15
});
xhr.open('GET', 'http://api.rss2json.com/v1/api.json', true);
xhr.onreadystatechange = () => {
xhr.onload = () => {
if(xhr.status === 200) {
console.log(xhr.response);
}
else {
console.log('error ' + xhr.status);
}
};
xhr.onerror = () => {
console.log('Connection error');
}
};
xhr.send(jsonData);
}
结果 - 错误
{"status":"error","message":"rss_url
需要参数"}
任何帮助将不胜感激。
如果你想用这些参数做一个GET
,只需把它们放在你给fetch()
或XHR的URL中:
let url = 'http://api.rss2json.com/v1/api.json?rss_url=https://news.yandex.ru/politics.rss&api_key=lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf&count=15';
我有一个简单的请求,使用 jQuery ajax()
getJson() {
$.ajax({
url: 'http://api.rss2json.com/v1/api.json',
method: 'GET',
dataType: 'json',
data: {
rss_url: 'https://news.yandex.ru/sport.rss',
api_key: 'lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf',
count: 15
}
})
// other code
}
这是对服务的请求,需要 rss(来自 rss_url)和 returns JSON。一切都很好,但现在我必须在没有 jQuery 的情况下发出相同的 ajax 请求。 此请求的所有参数都必须像这样(因为服务只理解这些参数)。
我尝试使用 fetch() 做同样的事情:
getJson() {
let url = 'http://api.rss2json.com/v1/api.json';
let config = {
method: 'GET',
dataType: 'json',
data: {
'rss_url': 'https://news.yandex.ru/politics.rss',
'api_key': 'lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf',
count: 15
}
}
fetch(url, config)
.then(response => {
console.log(response);
})
.catch(error => {
console.log(error);
});
}
尝试使用 XMLHttpRequest
getJson() {
let xhr = new XMLHttpRequest();
let jsonData = JSON.stringify({
rss_url: 'https://news.yandex.ru/politics.rss',
api_key: 'lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf',
count: 15
});
xhr.open('GET', 'http://api.rss2json.com/v1/api.json', true);
xhr.onreadystatechange = () => {
xhr.onload = () => {
if(xhr.status === 200) {
console.log(xhr.response);
}
else {
console.log('error ' + xhr.status);
}
};
xhr.onerror = () => {
console.log('Connection error');
}
};
xhr.send(jsonData);
}
结果 - 错误
{"status":"error","message":"rss_url
需要参数"}
任何帮助将不胜感激。
如果你想用这些参数做一个GET
,只需把它们放在你给fetch()
或XHR的URL中:
let url = 'http://api.rss2json.com/v1/api.json?rss_url=https://news.yandex.ru/politics.rss&api_key=lpdljjsrqnqsmtp0zqbngg0yjoeykl0iiw6f5lvnf&count=15';