简体   繁体   English

使用Jquery 1.10.2的跨域请求

[英]Cross-domain request with Jquery 1.10.2

I am making the following request: 我提出以下要求:

function doPoolPartyGetChildrenAjaxRequest(parent) {

return $.ajax({

    url: "http://127.0.0.1:8086/PoolParty/api/thesaurus/1DCE2E49-7DD8-0001-524C-1A1B14A0141A/childconcepts",

    data: {language: "en", parent: parent, properties: "skos:narrower"},

    username: 'xxxx',
    password: 'xxxx',

    dataType: 'json',

    crossDomain: true,


    beforeSend: function (req) {
        req.setRequestHeader('Authorization', 'Basic ' + btoa('superadmin:poolparty'));
    },

    xhrFields: {
        withCredentials: true
    },

    error: function (jqXHR, textStatus, errorThrown) {
        console.log(textStatus);
    },

    success: function (data) {

        for (var i = 0; i < data.length; i++) {
            data[i].title = data[i].prefLabel

            if (!(data[i].narrowers === undefined)) {
                data[i].lazy = true
            }
        }

        data.sort(function(a, b) {

            if (a.prefLabel.toLowerCase() == b.prefLabel.toLowerCase())
                return 0;
            if (a.prefLabel.toLowerCase() > b.prefLabel.toLowerCase())
                return 1;
            else
                return -1

        });

    }
})

What do i need to do to have it working, here is the error that i am getting: 我需要做什么才能使其正常工作,这是我得到的错误:

OPTIONS http://127.0.0.1:8086/PoolParty/api/thesaurus/1DCE2E49-7DD8-0001-524C-1A1B …Fthesaurus.iadb.org%2Fpublicthesauri%2FIdBTopics&properties=skos%3Anarrower 401 (Unauthorized) m.ajaxTransport.send 选项http://127.0.0.1:8086/PoolParty/api/thesaurus/1DCE2E49-7DD8-0001-524C-1A1B…Fthesaurus.iadb.org%2Fpublicthesauri%2FIdBTopics&properties = skos%3Anarrower 401(未经授权)m.ajaxTransport.send
m.extend.ajax doPoolPartyGetChildrenAjaxRequest setPoolPartyTreeBroswer m.extend.ajax doPoolPartyGetChildrenAjaxRequest setPoolPartyTreeBroswer
(anonymous function) (匿名功能)
m.Callbacks.j m.Callbacks.k.fireWith m.Callbacks.j m.Callbacks.k.fireWith
m.extend.ready 准备好扩展
J XMLHttpRequest cannot load http://127.0.0.1:8086/PoolParty/api/thesaurus/1DCE2E49-7DD8-0001-524C-1A1B14A0141A/childconcepts?language=en&parent=http%3A%2F%2Fthesaurus.iadb.org%2Fpublicthesauri%2FIdBTopics&properties=skos%3Anarrower . J XMLHttpRequest无法加载http://127.0.0.1:8086/PoolParty/api/thesaurus/1DCE2E49-7DD8-0001-524C-1A1B14A0141A/childconcepts?language=en&parent=http%3A%2F%2Fthesaurus.iadb.org%2Fpublicthesauri% 2FIdBTopics&properties = skos%3Anarrower No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:63342 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:63342 '。 The response had HTTP status code 401. error 响应的HTTP状态码为401。错误

I don't understand the error well. 我不太了解这个错误。 How can i see if my code is sending the right headers to the server ? 我如何查看我的代码是否将正确的标头发送到服务器? How can i see what the server respond. 我如何查看服务器的响应。

My Tomcat 7 config is as follows: 我的Tomcat 7配置如下:

<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>*</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>

I don't know what else i have to do. 我不知道我还要做什么。 Please can anyone guide me on this. 请任何人指导我。

How can i check things out, what is send receive. 我怎样才能检查出什么,什么是发送接收。 Is there anything particular with jquery 1.10.2. jQuery 1.10.2有什么特别之处。

Is there something wrong with the corsfilter of Tomcat 7 ? Tomcat 7的corsfilter出问题了吗?

I had working in the past with tomcat6. 我过去曾与tomcat6合作。 Since then i change to tomcat 7, and it simply does not work. 从那时起,我更改为tomcat 7,它根本无法正常工作。

Are you running Chrome with CORS disabled? 您是否在禁用CORS的情况下运行Chrome? Try running from the command line: 尝试从命令行运行:

google-chrome --disable-web-security

For more information on how to disable web security, check here 有关如何禁用网络安全性的更多信息,请单击此处

You need to set the Access-Control-Allow-Origin response header also to get CORS work 您还需要设置Access-Control-Allow-Origin响应标头以使CORS工作

Try 尝试

 headers.add("Access-Control-Allow-Origin", "*");

I have done this earlier like this in Spring MVC. 我这样做早些时候像这样 Spring MVC中。

package org.springframework.web.servlet.support;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        response.addHeader("Access-Control-Allow-Origin", "*");        
        if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())); {
            // CORS "pre-flight" request
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.addHeader("Access-Control-Allow-Headers", "Authorization");        
            response.addHeader("Access-Control-Max-Age", "1728000");
        }
        filterChain.doFilter(request, response);
    }

}

// example web.xml configuration

/*
  <filter>
    <filter-name>cors</filter-name>
    <filter-class>org.springframework.web.servlet.support.CorsFilter</filter-class>
  </filter>

  <filter-mapping>
    <filter-name>cors</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
*/

I also did what is mentioned here 我也做了这里提到的

package com.zhentao;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
public class CorsFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
            // CORS "pre-flight" request
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            response.addHeader("Access-Control-Allow-Headers", "Content-Type");
            response.addHeader("Access-Control-Max-Age", "1800");//30 min
        }
        filterChain.doFilter(request, response);
    }
}

The web.xml needs adding the following too: web.xml也需要添加以下内容:

  <filter>
    <filter-name>cors</filter-name>
    <filter-class>com.zhentao.CorsFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>cors</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

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

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