AJAX和纯Javascript
是否有任何代码类似于底部的代码,但在常规js中,而不是在jquery中? 谢谢大家!AJAX和纯Javascript,javascript,jquery,ajax,Javascript,Jquery,Ajax,是否有任何代码类似于底部的代码,但在常规js中,而不是在jquery中? 谢谢大家! $(function(){ $.ajax({ url: 'http://localhost:28017/local/andyb', type: 'get', dataType: 'jsonp', jsonp: 'jsonp', // mongod is expecting the parameter name to be called "jsonp" success: function (d
$(function(){
$.ajax({
url: 'http://localhost:28017/local/andyb',
type: 'get',
dataType: 'jsonp',
jsonp: 'jsonp', // mongod is expecting the parameter name to be called "jsonp"
success: function (data) {
console.log('success', data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log('error', errorThrown);
}
});
您可以使用FetchAPI,它是Web API的一部分,所以如果您想将其用于节点,则需要执行特殊的导入
async function postData(url = '', data = {}) {
// Default options are marked with *
const response = await fetch(url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc.
mode: 'cors', // no-cors, *cors, same-origin
cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
credentials: 'same-origin', // include, *same-origin, omit
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
redirect: 'follow', // manual, *follow, error
referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
return response.json(); // parses JSON response into native JavaScript objects
}
postData('https://example.com/answer', { answer: 42 })
.then(data => {
console.log(data); // JSON data parsed by `response.json()` call
});
您好,欢迎来到公司。您能重新安排一下您的句子吗?我们不知道您遇到了什么问题以及您想做什么。如果您需要JSONP,为什么不将其作为JSONP加载到脚本标记中?您是否可以根据OP的代码定制您的答案,而不是只是转储一个“是”的副本,但我认为代码中的注释足以让作者自己完成并学习一些东西,而不是复制,粘贴准备好的答案。我粘贴了代码,以节省他进行外部链接的时间。