尝试访问网络时 GET 不起作用 api
GET not working when trying to access web api
我在 .net 中写了一个简单的网站 api 并且 运行 它在我在 Windows 10 上的 iis 下创建的网站上。如果我把 url输入 Chrome "https://localhost:64591/api/custom",json 传递从数据库调用的记录。没问题。
我将相同的 url 放入 XMLHttpRequest 对象中,但 onload 事件从未发生。作为测试,我用我知道有效的外部 url 替换了 url。 onload 事件触发,响应对象工作。 iis 好像出了什么问题,它阻止了“获取”请求。
下面是代码。这是有效的外部 url:https://ghibliapi.herokuapp.com/films .....
通过 iis 访问 api 肯定有问题。
var request = new XMLHttpRequest();
request.open('GET', 'https://localhost:64591/api/custom', true)
request.onload = function ()
{
alert(this.response);
}
request.send();
试试这个,对我有用(我正在使用 python 服务器,如果你也想看到它,请评论)
url="127.0.0.1"; // or your loaclhost etc
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText;
}
};
xhttp.open("GET", url, true);
xhttp.send();
您可能遇到了 CORS 问题。
尝试让服务器发回 Access-Control-Allow-Origin: *
作为 header 的一部分。
我对 IIS 几乎一无所知,但 this 可能会有所帮助。
看起来这是一个 CORS 问题。我可以从 html 页面执行 GET 和 POST,只要该页面来自与我的网站 api 相同的服务器,并且我没有在字符串 [=] 中提及服务器12=] 而不是 'https://localhost:64591/api/custom'。我对此很满意,因为我只是在练习网络 api 的东西。虽然在某些时候我必须学习如何在 iis 中配置 CORS 模块。
我在 .net 中写了一个简单的网站 api 并且 运行 它在我在 Windows 10 上的 iis 下创建的网站上。如果我把 url输入 Chrome "https://localhost:64591/api/custom",json 传递从数据库调用的记录。没问题。
我将相同的 url 放入 XMLHttpRequest 对象中,但 onload 事件从未发生。作为测试,我用我知道有效的外部 url 替换了 url。 onload 事件触发,响应对象工作。 iis 好像出了什么问题,它阻止了“获取”请求。
下面是代码。这是有效的外部 url:https://ghibliapi.herokuapp.com/films ..... 通过 iis 访问 api 肯定有问题。
var request = new XMLHttpRequest();
request.open('GET', 'https://localhost:64591/api/custom', true)
request.onload = function ()
{
alert(this.response);
}
request.send();
试试这个,对我有用(我正在使用 python 服务器,如果你也想看到它,请评论)
url="127.0.0.1"; // or your loaclhost etc
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = xhttp.responseText;
}
};
xhttp.open("GET", url, true);
xhttp.send();
您可能遇到了 CORS 问题。
尝试让服务器发回 Access-Control-Allow-Origin: *
作为 header 的一部分。
我对 IIS 几乎一无所知,但 this 可能会有所帮助。
看起来这是一个 CORS 问题。我可以从 html 页面执行 GET 和 POST,只要该页面来自与我的网站 api 相同的服务器,并且我没有在字符串 [=] 中提及服务器12=] 而不是 'https://localhost:64591/api/custom'。我对此很满意,因为我只是在练习网络 api 的东西。虽然在某些时候我必须学习如何在 iis 中配置 CORS 模块。