錯誤提示
Access to XMLHttpRequest at 'http://localhost:8080/api/user/login' from origin 'http://localhost
解決方案
1. HTML頁面
ajax請求加入contentType: "application/x-www-form-urlencoded"
請求如下:
$.ajax({
type: "post",
url: 'url',
contentType: "application/x-www-form-urlencoded",
data: {"gameCode": 106, type: "2",version:1,uid:1},
dataType: "json",
success: function (data,status) {
console.log(data);
}
});
2. php程式
PHP檔案中加入請求頭部 header('Access-Control-Allow-Origin: *') ;
<?php
header('Access-Control-Allow-Origin: *');
$arr = [
array('id'=>1,'title'=>'one1'),
array('id'=>2,'title'=>'one2'),
array('id'=>3,'title'=>'one3'),
array('id'=>4,'title'=>'one4'),
];
echo json_encode($arr);
?>