Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Access-Control-Allow-Origin angularjs到php大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我的场景由两个webserver组成,一个是本地的,一个是远程的.

本地Web服务器(ApachE)处理一个Web应用程序,我想在其中向远程Web服务器(Lighttpd)发出ajax请求.

Ajax请求使用@L_197_3@ $http.

var req = {
    method: 'POST',url: 'http://url/myPHP.PHP',headers: {
        'Authorization': 'Basic ' + btoa('username:password'),'Content-Type': 'application/x-www-form-urlencoded'
    },xhrFields: {
       withCredentials: true
    },crossDomain: true,data: xmlString
}

$http(req).then(function () {
    console.log("OK!");
});

远程PHP脚本是:

<?PHP
    echo "You have CORS!";
?>

不幸的是我有一个

401 Unhauthorized
XMLhttprequest cAnnot load http://url/myPHP.PHP. Response to    preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had http status code 401.

远程Web服务器具有.htpasswd身份验证模式启用和配置的CORS请求.

关注一个lighttpd.conf

setenv.add-response-header = ( "Access-Control-Allow-Origin" => "*" )

解决方法

要使add-response-header在lighttpd中工作,必须在server.modules中启用mod_setenv.但是,您必须在mod_status之前启用此mod_setenv.

server.modules = (
    # ...
    "mod_fastcgi","mod_rewrite","mod_redirect","mod_setenv",## before mod_status
    "mod_status",# ...
)

或者,您可以使用PHP输出cors标头

<?PHP
header("Access-Control-Allow-Origin: *");
?>

我还想补充一点,如果您要发送http basic / digest auth数据,则不能使用通配符作为原点.您必须使用实际的源域

setenv.add-response-header = ( "Access-Control-Allow-Origin" => "example.com" )
setenv.add-response-header = ( "Access-Control-Allow-Credentials" => "true" )

大佬总结

以上是大佬教程为你收集整理的Access-Control-Allow-Origin angularjs到php全部内容,希望文章能够帮你解决Access-Control-Allow-Origin angularjs到php所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。