Javascript 带 301 重定向的 GET 请求
Javascript GET Request with 301 Redirect
在无法弄清楚出了什么问题之后,我想我会在这里碰碰运气,看看是否有人知道发生了什么。
我有一个带有 GoLang/Gorilla mux 服务器后端的 angularJS 应用程序。
Web 应用程序在 http://localhost:8888/ and the server, http://localhost:8080/
基本上我有这个简单的 Javascript GET 请求:
$.get('http://localhost:8080/api/v1'+'/locations/', {borough: "Queens"})
使用检查元素,在响应 headers 中我可以看到以下内容:
Content-Length:68
Content-Type:text/html; charset=utf-8
Date:Sun, 17 Apr 2016 20:12:00 GMT
Location:/api/v1/locations?borough=queens
以及控制台中的以下内容:
XMLHttpRequest cannot load http://localhost:8080/api/v1/locations/? borough=queens. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.
我使用 Postman 尝试完全相同的请求并看到 401:
Access-Control-Allow-Origin →*
Content-Length →0
Content-Type →text/plain; charset=utf-8
Date →Sun, 17 Apr 2016 19:46:53 GMT
这是我所期望的。出于某种原因,似乎我的请求甚至没有到达服务器,该服务器不提供 301 请求。 Postman 和应用给出完全不同的响应这一事实令人费解。
我在使用 CORS 时遇到了一些问题,但我认为我已经解决了。 Postman 给了我 Access-Control-Allow-Origin header 不同的结果,这让我觉得这是另外一回事。
欢迎任何帮助。
编辑:
我让服务器回复了标准的 Cors 参数。这是我现在从 Postman 那里得到的:
Access-Control-Allow-Headers →Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods →GET, POST, PUT, DELETE
Access-Control-Allow-Origin →*
Content-Length →0
Content-Type →text/plain; charset=utf-8
Date →Sun, 17 Apr 2016 20:27:31 GMT
仍然为 JS 调用获取 301...
Edit2:(尝试将 Postman 调用设置为与网络请求完全相同)
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:8888
Referer:http://localhost:8888/root/mainapp/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
仍然从 inspect element 得到相同的 301,而 Postman 得到预期的 401。为什么在 Restful HTTP 上下文中,我们有两个相同的 HTTP 调用,却有完全不同的行为?!
所以基本上当我删除最后一个斜杠时:
来自
$.get('http://localhost:8080/api/v1'+'/locations/', {borough: "Queens"})
至
$.get('http://localhost:8080/api/v1'+'/locations', {borough: "Queens"})
这成功了!!!我不知道为什么。如果我想听听任何可能知道这里发生了什么以及为什么这样做的人的意见。我会重申 - 由于最后一个斜杠,请求甚至没有到达服务器。
在无法弄清楚出了什么问题之后,我想我会在这里碰碰运气,看看是否有人知道发生了什么。
我有一个带有 GoLang/Gorilla mux 服务器后端的 angularJS 应用程序。 Web 应用程序在 http://localhost:8888/ and the server, http://localhost:8080/
基本上我有这个简单的 Javascript GET 请求:
$.get('http://localhost:8080/api/v1'+'/locations/', {borough: "Queens"})
使用检查元素,在响应 headers 中我可以看到以下内容:
Content-Length:68
Content-Type:text/html; charset=utf-8
Date:Sun, 17 Apr 2016 20:12:00 GMT
Location:/api/v1/locations?borough=queens
以及控制台中的以下内容:
XMLHttpRequest cannot load http://localhost:8080/api/v1/locations/? borough=queens. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access.
我使用 Postman 尝试完全相同的请求并看到 401:
Access-Control-Allow-Origin →*
Content-Length →0
Content-Type →text/plain; charset=utf-8
Date →Sun, 17 Apr 2016 19:46:53 GMT
这是我所期望的。出于某种原因,似乎我的请求甚至没有到达服务器,该服务器不提供 301 请求。 Postman 和应用给出完全不同的响应这一事实令人费解。
我在使用 CORS 时遇到了一些问题,但我认为我已经解决了。 Postman 给了我 Access-Control-Allow-Origin header 不同的结果,这让我觉得这是另外一回事。
欢迎任何帮助。
编辑:
我让服务器回复了标准的 Cors 参数。这是我现在从 Postman 那里得到的:
Access-Control-Allow-Headers →Origin, X-Requested-With, Content-Type, Accept, Authorization
Access-Control-Allow-Methods →GET, POST, PUT, DELETE
Access-Control-Allow-Origin →*
Content-Length →0
Content-Type →text/plain; charset=utf-8
Date →Sun, 17 Apr 2016 20:27:31 GMT
仍然为 JS 调用获取 301...
Edit2:(尝试将 Postman 调用设置为与网络请求完全相同)
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Origin:http://localhost:8888
Referer:http://localhost:8888/root/mainapp/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
仍然从 inspect element 得到相同的 301,而 Postman 得到预期的 401。为什么在 Restful HTTP 上下文中,我们有两个相同的 HTTP 调用,却有完全不同的行为?!
所以基本上当我删除最后一个斜杠时:
来自
$.get('http://localhost:8080/api/v1'+'/locations/', {borough: "Queens"})
至
$.get('http://localhost:8080/api/v1'+'/locations', {borough: "Queens"})
这成功了!!!我不知道为什么。如果我想听听任何可能知道这里发生了什么以及为什么这样做的人的意见。我会重申 - 由于最后一个斜杠,请求甚至没有到达服务器。