IIS运行PHP ajax调用跨域问题

图片上传的问

PHP上传的时候会创建一个临时文件

在php.ini 设置extension_dir = "C:\windows\temp" 的文章,因为你上传的图片会继承这个文件的权限,所以要给这个文件加添加权限  everyone,IUSER 的读写权限,不然不可见,上传到的文件夹也要添加权限!

 

跨域问题

直接价格web.config  这个文件,要添加多个域名用*号,

Access-Control-Allow-Credentials: true

如果服务端不设置响应头,响应会被忽略不可用;同时,服务端需指定一个域名(Access-Control-Allow-Origin:www.baidu.com),而不能使用泛型(Access-Control-Allow-Origin: *)也就是不能设置*,多域名失效

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, api_key, Authorization"/>
<add name="Access-Control-Allow-Credentials" value="true"/>
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

 

js部分

return new Promise((resolve,reject)=>{
let requestObj;
if(window.XMLHttpRequest){
requestObj=new XMLHttpRequest();
}else{
requestObj=new ActiveXObject;
}

requestObj.open('POST', url, true);
//requestObj.withCredentials = true;
//requestObj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
requestObj.send(data);

requestObj.onreadystatechange=()=>{
if(requestObj.readyState==4){
if(requestObj.status==200){
let obj=requestObj.response;
if(typeof obj!=='object'){
obj=JSON.parse(obj);
}
resolve(obj);

}else{
reject(requestObj);
}
}
}

})

posted @ 2018-01-24 01:39  luoyiming  阅读(256)  评论(0)    收藏  举报