鍍金池/ 問答/Linux  網(wǎng)絡(luò)安全  HTML/ Fetch 跨域求情失敗?

Fetch 跨域求情失敗?

前端地址 localhost:8080
接口地址:http://www.haoyinguoji.com/ph...

圖片描述
沒有得到值
圖片描述

import {
    baseUrl
} from './env'

export default async(url = '', data = {}, type = 'GET', method = 'fetch') => {
    type = type.toUpperCase();
    url = baseUrl + url;

    if (type == 'GET') {
        debugger;
        let dataStr = ''; //數(shù)據(jù)拼接字符串
        Object.keys(data).forEach(key => {
            dataStr += key + '=' + data[key] + '&';
        })

        if (dataStr !== '') {
            dataStr = dataStr.substr(0, dataStr.lastIndexOf('&'));
            url = url + '?' + dataStr;
        }
    }

    if (window.fetch && method == 'fetch') {
        let requestConfig = {
            credentials: 'include',
            method: type,
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            mode: "no-cors",
            cache: "force-cache"
        }

        if (type == 'POST') {
            Object.defineProperty(requestConfig, 'body', {
                value: JSON.stringify(data)
            })
        }

        debugger;

        try {
            const response = await fetch(url, requestConfig);
            const responseJson = await response.json();
            return responseJson
        } catch (error) {
            throw new Error(error)
        }
    } else {
        return new Promise((resolve, reject) => {
            let requestObj;
            if (window.XMLHttpRequest) {
                requestObj = new XMLHttpRequest();
            } else {
                requestObj = new ActiveXObject;
            }

            let sendData = '';
            if (type == 'POST') {
                sendData = JSON.stringify(data);
            }

            requestObj.open(type, url, true);
            requestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            requestObj.send(sendData);

            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)
                    }
                }
            }
        })
    }
}
    export const accountLogin = (phone_num, user_pwd) => fetch('http://www.haoyinguoji.com/php/user.php?act=login', {
        phone_num,
        user_pwd
    },'POST');

php后臺活得的參數(shù),我不知道是我這里改還是后臺啊~

array(1) {
  ["act"]=>
  string(5) "login"
}
回答
編輯回答
網(wǎng)妓

服務(wù)端加上一個允許跨域的響應(yīng)頭就好了

2017年1月25日 09:58
編輯回答
乞許

這種情況是因為對方的服務(wù)器只允許特定的域名請求,如果這是你自己的服務(wù)器,最簡單的方法是服務(wù)端將響就頭設(shè)置成 Access-Control-Allow-Origin:域名 如果不是你的服務(wù)器,可以考慮自己寫一個服務(wù)器做中轉(zhuǎn),轉(zhuǎn)發(fā)一下你的請求就可以了

2017年2月21日 04:14