JavaScript 使用 XMLHttpRequest 获取 URL 并处理响应数据 - 获取 name2 值
function getName2Value(geturl) {\n return new Promise((resolve, reject) => {\n const xhr = new XMLHttpRequest();\n xhr.open('GET', geturl);\n xhr.onreadystatechange = function() {\n if (xhr.readyState === XMLHttpRequest.DONE) {\n if (xhr.status === 200) {\n const response = JSON.parse(xhr.responseText);\n const name2 = response['url'];\n resolve(name2);\n } else {\n reject(xhr.status);\n } \n }\n };\n xhr.send();\n });\n}\n\n// 使用示例\ngetName2Value('example.com/api/url')\n .then(name2 => {\n console.log(name2);\n })\n .catch(error => {\n console.error('Error:', error);\n });
原文地址: https://www.cveoy.top/t/topic/p4MH 著作权归作者所有。请勿转载和采集!