Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将curl命令转换为ajax请求_Php_Jquery_Ajax_Authentication_Curl - Fatal编程技术网

Php 将curl命令转换为ajax请求

Php 将curl命令转换为ajax请求,php,jquery,ajax,authentication,curl,Php,Jquery,Ajax,Authentication,Curl,关于这个问题,我正在尝试将curl语句转换为ajax请求 我的请求正在使用以下curl命令: curl --user XXXX:YYYY "URL" 但在jquery ajax中尝试使用它是行不通的,我的代码如下所示: $.ajax({ url : "URL", type : 'GET', dataType : 'json', contentType : 'application/json', username: "XXXX", password

关于这个问题,我正在尝试将curl语句转换为ajax请求

我的请求正在使用以下curl命令:

curl --user XXXX:YYYY "URL"
但在jquery ajax中尝试使用它是行不通的,我的代码如下所示:

$.ajax({
    url : "URL",
    type : 'GET',
    dataType : 'json',
    contentType : 'application/json',
    username: "XXXX",
    password: "YYYY",
    success : function(data) {
        console.log(JSON.stringify(data));
    },
    error : function() {
        console.log("Cannot get data");
    }
});
我还尝试设置beforeSend值,如下所示:

$.ajax({
    url : "URL",
    type : "GET",
    processData: false,
    dataType : 'json',
    headers : {
        'Accept' : 'application/json',
        'Content-Type' : 'application/json'
    },
    data : JSON.stringify(text),
    withCredentials : true,
    beforeSend : function(xhr) {
        xhr.setRequestHeader("Authorization", "Basic " + btoa("XXXX:YYYY"));
    }
}).then(function(data) {
    console.log('data', data);
}, function(err) {
    console.log('err', err);
});
在这两种情况下,我都会得到相同的错误:

无法加载XMLHttpRequest “网址”。回应 飞行前的HTTP状态代码401无效

有人知道如何解决这个问题吗

更新

标题如下所示:

概述

Request URL: "URL"
Request Method:OPTIONS
Status Code:401 Unauthorized
Remote Address:***
响应标题

Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Connection:Keep-Alive
Content-Language:en
Content-Length:1061
Content-Type:text/html;charset=utf-8
Date:Wed, 10 Feb 2016 13:12:34 GMT
Expires:0
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache
Set-Cookie:JSESSIONID=CE27E7E84651D519BD99802B65C431EB; Path=/matchbox-webservice/; Secure
Strict-Transport-Security:max-age=31536000 ; includeSubDomains
WWW-Authenticate:Basic realm="Realm"
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,pt;q=0.2,nb;q=0.2,da;q=0.2
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:***
Origin:http://evil.com/
Pragma:no-cache
Referer:http://localhost:8080/templates/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36
请求标题

Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Connection:Keep-Alive
Content-Language:en
Content-Length:1061
Content-Type:text/html;charset=utf-8
Date:Wed, 10 Feb 2016 13:12:34 GMT
Expires:0
Keep-Alive:timeout=5, max=100
Pragma:no-cache
Server:Apache
Set-Cookie:JSESSIONID=CE27E7E84651D519BD99802B65C431EB; Path=/matchbox-webservice/; Secure
Strict-Transport-Security:max-age=31536000 ; includeSubDomains
WWW-Authenticate:Basic realm="Realm"
X-Content-Type-Options:nosniff
X-Frame-Options:DENY
X-XSS-Protection:1; mode=block
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,pt;q=0.2,nb;q=0.2,da;q=0.2
Access-Control-Request-Headers:accept, authorization, content-type
Access-Control-Request-Method:GET
Cache-Control:no-cache
Connection:keep-alive
Host:***
Origin:http://evil.com/
Pragma:no-cache
Referer:http://localhost:8080/templates/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36

我在服务器端添加了一个CORS筛选器,如下所示,但仍然出现此错误:

XMLHttpRequest无法加载“URL”。对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此,不允许访问源“”。响应的HTTP状态代码为401

我的CORS定义有问题吗

@WebFilter(urlPatterns = "/*", filterName = "simpleCORSFilter")
public class SimpleCORSFilter implements Filter {
private static Logger logger = LoggerFactory.getLogger(SimpleCORSFilter.class);

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    HttpServletRequest request = (HttpServletRequest) req;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
    response.setHeader("Access-Control-Max-Age", "10"); // in seconds
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
    logger.info("doFilter() called: " + request.getMethod());
    chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {
}

public void destroy() {
}

}

我在服务器端添加了一个CORS筛选器,如下所示,但仍然出现此错误:

XMLHttpRequest无法加载“URL”。对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。因此,不允许访问源“”。响应的HTTP状态代码为401

我的CORS定义有问题吗

@WebFilter(urlPatterns = "/*", filterName = "simpleCORSFilter")
public class SimpleCORSFilter implements Filter {
private static Logger logger = LoggerFactory.getLogger(SimpleCORSFilter.class);

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    HttpServletRequest request = (HttpServletRequest) req;
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
    response.setHeader("Access-Control-Max-Age", "10"); // in seconds
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
    logger.info("doFilter() called: " + request.getMethod());
    chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {
}

public void destroy() {
}

}

401表示服务器由于授权问题拒绝回复。注意:不是身份验证,而是授权。你真的确定这是对服务器的合法请求吗?如果预飞行失败,听起来接收域不支持CORS,这意味着你无法从JS发出请求。如果不支持CORS,curl命令不会失败吗?@fsulser否不会。Curl不执行任何有关COR的检查。浏览器检查CORS头。您可以尝试在浏览器中检查开发人员控制台,以获取ajax调用的所有标题sending@fsulser--关于为什么cURL不受同源策略的限制,因此不需要CORS。401表示服务器由于授权问题而拒绝回复。注意:不是身份验证,而是授权。你真的确定这是对服务器的合法请求吗?如果预飞行失败,听起来接收域不支持CORS,这意味着你无法从JS发出请求。如果不支持CORS,curl命令不会失败吗?@fsulser否不会。Curl不执行任何有关COR的检查。浏览器检查CORS头。您可以尝试在浏览器中检查开发人员控制台,以获取ajax调用的所有标题sending@fsulser--关于为什么cURL不受同源策略的限制,因此不需要COR。