API get请求未在javascript中解析
我想知道是不是因为它试图访问两个IP?很抱歉,如果这是一个noob问题,但是当我点击我的API时,我内置了postman,或者即使我只是将get请求url扔到任何浏览器中,它也会返回JSON数据。我有点想不通为什么我不能解决这个问题API get请求未在javascript中解析,javascript,json,Javascript,Json,我想知道是不是因为它试图访问两个IP?很抱歉,如果这是一个noob问题,但是当我点击我的API时,我内置了postman,或者即使我只是将get请求url扔到任何浏览器中,它也会返回JSON数据。我有点想不通为什么我不能解决这个问题 const app = document.getElementById('root') const logo = document.createElement('img') logo.src = 'logo.png' const container = docu
const app = document.getElementById('root')
const logo = document.createElement('img')
logo.src = 'logo.png'
const container = document.createElement('div')
container.setAttribute('class', 'container')
app.appendChild(logo)
app.appendChild(container)
var request = new XMLHttpRequest()
request.open('GET', '18.220.177.84:8080/election_results?state=Ohio&year=2016&county=Cuyahoga', true)
request.onload = function () {
// Begin accessing JSON data here
var data = JSON.parse(this.response)
if (request.status >= 200 && request.status < 400) {
data.forEach((election_results) => {
const card = document.createElement('div')
card.setAttribute('class', 'card')
const h1 = document.createElement('h1')
h1.textContent = election_results.county
const p = document.createElement('p')
election_results.candidate = election_results.candidate.substring(0, 300)
p.textContent = `${election_results.candidate}...`
container.appendChild(card)
card.appendChild(h1)
card.appendChild(p)
})
} else {
const errorMessage = document.createElement('marquee')
errorMessage.textContent = `Gah, it's not working!`
app.appendChild(errorMessage)
}
}
request.send()
const-app=document.getElementById('root'))
const logo=document.createElement('img')
logo.src='logo.png'
const container=document.createElement('div')
container.setAttribute('class','container')
append.child(徽标)
app.appendChild(容器)
var请求=新的XMLHttpRequest()
请求打开('GET','18.220.177.84:8080/选举结果?州=俄亥俄州&年份=2016年&县=库亚霍加',对)
request.onload=函数(){
//从这里开始访问JSON数据
var data=JSON.parse(this.response)
如果(request.status>=200&&request.status<400){
data.forEach((选举结果)=>{
const card=document.createElement('div')
setAttribute('class','card')
常量h1=document.createElement('h1')
h1.textContent=选举结果.county
常量p=document.createElement('p')
选举结果.候选人=选举结果.候选人.子字符串(0,300)
p、 textContent=`${election_results.candidate}`
容器.子容器(卡)
卡.子卡(h1)
儿童卡(p)
})
}否则{
const errorMessage=document.createElement('marquee')
errorMessage.textContent=`Gah,它不工作了`
append.child(错误消息)
}
}
请求发送()
是因为我使用的是xml http请求吗?显然,在邮递员GET呼叫中混合了两个单独的IP地址 第一个是本地主机(
127.0.0.1:5500
),第二个是您试图访问的实际服务器(18.220.177.84:8080
)
GET调用应该只命中(
18.220.177.84:8080
)(删除第一部分,本地主机)URL解析器认为18.220.177.84:8080
是URL路径的一部分。在它前面加上/
(以及可选的https
方案来更改协议),让解析器知道这是URL的权限(在本例中为主机+端口)。因此,问题在于您的代码在不提及协议的情况下向URL发出get请求。在URL之前使用HTTP或HTTPS(以需要的为准)。所以这个网址是,
https://18.220.177.84:8080/election_results?state=Ohio&year=2016&county=Cuyahoga
希望这能解决你的问题
现在进入关于CORS错误的第二个问题。基本上,CORS或跨源资源共享是一种标准,允许服务器过滤特定的跨源请求,同时拒绝其他请求。在您的情况下,服务器阻止或拒绝您的请求。所以要解决这个问题,
访问控制允许原点:
。例如,访问控制允许原点:http://localhost:3000
。您可以通过指定
访问控制允许源代码:
,但我强烈建议您不要这样做
提供有关API的详细信息以及所需的代码。我添加了我的代码。。很抱歉,见上文。。我添加了我的代码片段。是因为我在发出http请求吗?这成功了!非常感谢。但我现在收到此消息..“跨源请求被阻止:同一源策略不允许在读取远程资源。(原因:CORS标头“访问控制允许源”丢失)。”这是一种安全机制。见: