简体   繁体   English

Ajax呼叫返回301突然突然移动

[英]Ajax call return 301 permanently moved suddenly

The below code snippet was working fine, but it stopped all of a sudden for no apparent reason 下面的代码段运行正常,但突然停止,没有明显原因

jQuery.ajax({
            url: "http://example.com/api/getstuff.php?Location="+location+"&token="+token,              
            type: 'GET',
            dataType: 'json',
            success:function(data){
            if(data.success == '0'){
                alert("success");
            }
            else
            {
            alert(data.error);
            }

        });

when I copy the url of api being called from inspector and open in the browser it works fine, it was working fine both on development and production. 当我复制从检查器调用的api的URL并在浏览器中打开时,它可以正常工作,在开发和生产上都可以正常工作。 any ideas? 有任何想法吗?

*edit The issue was fixed, the api call was missing www so the call was redirected to use www, hence the 301 error. * edit该问题已解决,api调用缺少www,因此该调用被重定向为使用www,因此出现301错误。

Though this fixed the problem but I'm unable of explaining because this was the way used long ago, suddenly it stopped working! 尽管这解决了问题,但由于这是很久以前使用的方式,所以我无法解释,突然它停止了工作!

Anyways I thought I should post the fix so someone can find useful. 无论如何,我认为我应该发布此修复程序,以便有人可以找到有用的信息。

Thanks 谢谢

This error usually occurs when the resource or url you are trying to access has been moved or redirected to another url. 当您尝试访问的资源或URL已被移动或重定向到另一个URL时,通常会发生此错误。

In this case, response from the web server always includes an alternate url. 在这种情况下,来自Web服务器的响应始终包含备用URL。

Update your code to send request to this alternate url. 更新您的代码以将请求发送到该备用网址。

If there is no alternate url found in the response then there might be some issue with the web server or url redirection is not properly setup at web server end. 如果在响应中找不到备用URL,则Web服务器可能存在问题,或者在Web服务器端未正确设置URL重定向。

As you said that the same url is working when you open it in the browser, carefully note which URL actually gets displayed, because browser may silently switch to a substitute URL if it receives an 301 message from the Web server. 正如您说的那样,当您在浏览器中打开该URL时,它是有效的,请仔细注意实际上显示了哪个URL,因为如果浏览器从Web服务器接收到301消息,则它可能会默默地切换到替代URL。

Is this your API or a third-party API? 这是您的API还是第三方API? This status code means that this resource was permanently moved to a new URL and can't be reached in this URL anymore. 此状态代码表示该资源已永久移动到新的URL,因此无法再通过该URL进行访问。 If it's not your own API, you must check the Location header to get the new resource location. 如果不是您自己的API,则必须检查Location标头以获取新的资源位置。 Use the inspector to check this header out. 使用检查器检查此标头。

Just to clarify, take a look at the RFC below: 为了澄清起见,请看下面的RFC:

Link: http://tools.ietf.org/html/rfc7231#section-6.4.2 链接: http//tools.ietf.org/html/rfc7231#section-6.4.2

301 Moved Permanently 301永久移动

The 301 (Moved Permanently) status code indicates that the target resource has been assigned a new permanent URI and any future references to this resource ought to use one of the enclosed URIs. 301(永久移动)状态码表示已为目标资源分配了新的永久URI,以后对该资源的任何引用都应使用其中的一个URI。 Clients with link-editing capabilities ought to automatically re-link references to the effective request URI to one or more of the new references sent by the server, where possible. 具有链接编辑功能的客户端应在可能的情况下自动将对有效请求URI的引用重新链接到服务器发送的一个或多个新引用。

This part talks about the Location header -> 这部分讨论位置标头->

The server SHOULD generate a Location header field in the response containing a preferred URI reference for the new permanent URI. 服务器应该在响应中生成一个Location头字段,其中包含新的永久URI的首选URI引用。 The user agent MAY use the Location field value for automatic redirection. 用户代理可以使用位置字段值进行自动重定向。 The server's response payload usually contains a short hypertext note with a hyperlink to the new URI(s). 服务器的响应有效负载通常包含一个简短的超文本注释,其中包含指向新URI的超链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM