使用绝对网址和相对网址在JavaScript中进行抓取 [英] Fetching in javascript with absolute url and relative url

查看:68
本文介绍了使用绝对网址和相对网址在JavaScript中进行抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在本地服务器上运行的脚本,该脚本可获取一个php文件(也在本地服务器上).如果我写要获取的url作为相对路径,则文件不会出现问题,但是,如果我添加了127.0.0.1/mypath/myFile,则会出现403错误.

I have a script that I run on my local server and that fetches a php file (on the local server too). If I write the url to fetch as a relative path, I get the file without problems, but, if I add the 127.0.0.1/mypath/myFile, I get a 403 error.

function localServerCall() {
    var urlLocalServer = '127.0.0.1:8000/mypath/myfile.php';
    //var urlLocalServer = 'myfile.php';   //THIS WORKS!
    fetch(urlLocalServer).then(function(response) {
      console.log(response.json);
      return response.json();
  }).then(function(data) {
      console.log(data)
  }).catch(function(err) {
      console.log ('ERROR LOCALSERVER', err);
  })
}

我想知道在将绝对/相对网址与fetch一起使用时是否存在某些限制,或者这个问题是否可能是由于其他原因引起的.

I was wondering if there are some limitations on the use of absolute/relative urls with fetch or if this problem might be due to something else.

推荐答案

不是以方案或以//开头的URL被视为 path (相对URL的一部分) ).

A URL that doesn't start with a scheme or with // is treated as a path (part of a relative URL).

http://example.com/foo/中,您的URL解析为http://example.com/foo/127.0.0.1:8000/mypath/myfile.php.

From http://example.com/foo/, your URL resolves to http://example.com/foo/127.0.0.1:8000/mypath/myfile.php.

您几乎可以肯定想要:

var urlLocalServer = 'http://127.0.0.1:8000/mypath/myfile.php';

这篇关于使用绝对网址和相对网址在JavaScript中进行抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆