对RESTlet的XHR请求失败,在工作中键入它

时间:2018-02-17 13:39:04

标签: rest dojo xmlhttprequest

我正在接受RESTlet的挑战,我可以在http://localhost:8080/asWeb/r/WebVersion访问我的RESTful应用程序的URL并检索JSON格式的文本,当SSH端口转发到我的盒子时,我看到Dojo XHR在我的手机上运行。问题是每当我从本地主机运行时我都会收到错误:

STATUS:0(RequestError:无法加载http://localhost:8080/asWeb/r/WebVersion状态:0)

{...}消息:“无法加载/ asWeb / r /登录状态:0”响应:{...} getHeader:function _420()选项:{...}数据:“”handleAs:“text”ioArgs:{ ...} args:Object {url:“/ asWeb / r / Login”,handleAs:“application / json”,load:load(),...} error:Object {message:“无法加载/ asWeb / r /登录状态:0“,堆栈:”_ 31e @ http://localhost:8080/asWeb/js/dojo/dojo/dojo.js:8:71685 \ n_418 @ http://localhost:8080/asWeb/js/dojo/dojo/dojo.js:8:92086 \ n“,状态:0,...}句柄:”application / json“查询:”“url:”/ asWeb / r /登录“xhr:XMLHttpRequest {readyState:4,timeout:0,withCredentials:false,...} proto :Object {...} proto :Object {data:null,sync :false,方法:“GET”,...} status:0text:“”url:“/ asWeb / r / Login”xhr:XMLHttpRequest {readyState:4,timeout:0,withCredentials:false,...} proto < / strong>:{...} defineGetter :function defineGetter () defineSetter :function defineSetter () lookupGetter :函数 lookupGetter () lookupSetter :有趣ction的 lookupSetter ()构造:功能对象()hasOwnProperty:函数hasOwnProperty()isPrototypeOf:功能isPrototypeOf()propertyIsEnumerable:功能propertyIsEnumerable()的toLocaleString:函数的toLocaleString()toSource:功能toSource()的toString:函数toString()valueOf:function valueOf()responseText:“”stack:“_ 31e @ http://localhost:8080/asWeb/js/dojo/dojo/dojo.js:8:71685 \ n_418 @ http://localhost:8080/asWeb/js/dojo/dojo/dojo.js:8:92086 \ n”状态:0xhr:XMLHttpRequest {readyState:4,timeout:0,withCredentials: false,...} proto :对象{name:“RequestError”,构造函数:_31e(),stack:“”} dojo.js:8:56587

同样,这是在地址栏中粘贴它时应该返回的内容:

[{"Changes":"More backend work on Javacode.","Version":"14.19","Date":"2018-02-16"},{"Changes":"More work on Dojo/Java development","Version":"14.18","Date":"2018-02-15"},{"Changes":"More work on Dojo/Java deployment","Version":"14.17","Date":"2018-02-14"},{"Changes":"More work on Dojo/Java deployment","Version":"14.16","Date":"2018-02-13"},{"Changes":"Work on Dojo, work on Java version","Version":"14.15","Date":"2018-02-11"}]

我在网上跟踪了很多教程,将CORS集成到RESTlet中,验证我是否阻止了我的AJAX / XHR请求的默认,并且如果我从Firefox检查器面板复制并粘贴它,我可以访问该URL,因此可以获得有效数据。

https://github.com/f00dl3/asWeb

我在这里发布了大部分代码。我还包括一个“vanillaXhr.js”文件,它使用“vanilla”方式遇到同样的问题 - 所以这不是Dojo的错。

对于它的价值,这里的jQuery也失败了:

    $(document).ready(function() {
    $.ajax({
        url: baseForRestlet+"/Login",
        dataType: 'json',
        type : 'GET',
        success: function(data){
            window.alert(data);
        },
        error: function(jqXHR, textStatus, ajaxOptions, errorThrown, result) { 
            window.alert(" jqXHR: "+jqXHR+"\n textStatus: "+textStatus+"\n    errorThrown: "+errorThrown+"\n ajaxOptions: "+ajaxOptions+"\n result: "+result);
            console.log(jqXHR.status);
        }
    }); 

});

我在Netbeans中将CORS添加到我的web.inf中:

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
        <param-name>cors.allowed.origins</param-name>
        <param-value>http://localhost, https://localhost, http://127.0.0.1, https://127.0.0.1</param-value>
    </init-param>
    <init-param>
    <param-name>cors.allowed.methods</param-name>
    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>
  </init-param>
  <init-param>
    <param-name>cors.allowed.headers</param-name>
    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>
  </init-param>
  <init-param>
    <param-name>cors.exposed.headers</param-name>
    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
  </init-param>
  <init-param>
    <param-name>cors.support.credentials</param-name>
    <param-value>true</param-value>
  </init-param>
  <init-param>
    <param-name>cors.preflight.maxage</param-name>
    <param-value>10</param-value>
  </init-param>
</filter>
<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

在我的RESTlet中,我在Application JAVA文件中有以下内容:

public AnthonyRestlet() {

    CorsService corsService = new CorsService();
    corsService.setAllowingAllRequestedHeaders(true);
    corsService.setAllowedOrigins(new HashSet(Arrays.asList(
            "http://localhost",
            "https://localhost",
            "http://127.0.0.1",
            "https://127.0.0.1"
    )));
    corsService.setAllowedCredentials(true);
    corsService.setSkippingResourceForCorsOptions(true);
    getServices().add(corsService);

}

1 个答案:

答案 0 :(得分:0)

想出来,这真的很简单。

我所要做的就是在每个Resource Java文件中将@Get更改为@Get @Options

scalaVersion := 2.11.8