I have build basic authorization and cors in vps.
curl -X OPTIONS -i http://111.111.111.111
HTTP/1.1 200 OK
Date: Sat, 15 Sep 2018 08:07:37 GMT
Server: Apache/2.4.6 (CentOS)
Access-Control-Allow-Origin: http://127.0.0.1
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
Allow: OPTIONS,GET,HEAD,POST,TRACE
Content-Length: 0
Content-Type: httpd/unix-directory
curl -u xxxx:xxxx -i http://111.111.111.111/remote.php
HTTP/1.1 200 OK
Date: Sat, 15 Sep 2018 08:08:07 GMT
Server: Apache/2.4.6 (CentOS)
Access-Control-Allow-Origin: http://127.0.0.1
Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With
Last-Modified: Sat, 15 Sep 2018 07:54:13 GMT
ETag: "24-575e43f02c324"
Accept-Ranges: bytes
Content-Length: 36
<?php
echo '{"name","myname"}';
?>
You can see that authorization and cors are in good status.
The test-ajax-cors.html in my local directory /var/www/html.
<script src="http://127.0.0.1/jquery-3.3.1.js"></script>
<script>
function Ajax( ) {
var url = 'http://111.111.111.111/remote.php';
$.ajax(url, {
type:"post",
crossDomain: "true",
dataType:"json",
beforeSend:function(xhr) {
xhr.setRequestHeader('Authorization',"Basic " + btoa("xxxx:xxxx"))},
success:function(response){
data = JSON.stringify(response);
alert(data);
mytext = $("#remote");
mytext.append(data);
},
error: function (e) {
alert("error");
}
});
};
</script>
<input type="button" value="show content" onclick="Ajax();">
<p id="remote">the content on remote webpage</p>
The remote.php in http://111.111.111.111.
cat /var/www/html/remote.php
<?php
echo '{"name","myname"}';
?>
Type 127.0.0.1/test-ajax-cors.html ,click show content,
1.i got alert info: error
2.remote.php was called for two times by 127.0.0.1/test-ajax-cors.html (the show content button in 127.0.0.1/test-ajax-cors.html was clicked for just one time).
when to call remote.php for the first time, no content in remote.php's resposne.
The first request maybe a CORS-preflight request , browsers send an OPTIONS request without any Authorization header,and in my case the server send a 200 status code to browser,it means that everything is in good status.
The content in remote.php's response when to call remote.php for the second time.
How to make the content in second response to show by 127.0.0.1/test-ajax-cors.html.
Thank to Sally CJ's notice.
yum install mod_php
systemctl restart httpd
Type 127.0.0.1/test-ajax-cors.html ,click show content.
1.alert error info
2.remote.php were called for two times,all the response for remote.php are same.
{"name","myname"}
The content is what i expect,why can't show it in webpage 127.0.0.1\test-ajax-cors.html,which result the alert error info?





