获取API模式:no-cors然后分配函数变量

时间:2018-03-19 11:25:02

标签: javascript apache cors xmlhttprequest fetch

你好我创建了这个代码块,我不知道如何分配那个函数变量。

Api服务器: 192.168.1.109:8085

Nginx客户端服务器: 192.168.1.137:8086,192.168.1.109:8086

对于Cors,我的所有设置似乎都是正确的。我想:/。

主要职责是在使用fetch获取数据时验证和捕获数据。即使我成功,我也无法从屏幕上打印响应数据。请救救我我是堆栈,今天是我生命中的第九天:(

的httpd.conf

Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
Header always set Access-Control-Max-Age "1000"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
<IfModule mod_headers.c>
Header always set Access-Control-Allow-Credentials true
</IfModule>

用于获取数据的客户端应用程序代码。 设备-controller.js

angular
.module('interface', ['loadingStatus'])
.controller('DeviceController', DeviceController)
.config(['$httpProvider', function($httpProvider) {
    //$httpProvider.defaults.withCredentials = true;
    //delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);

function DeviceController($ scope,$ http){

$scope.postLogin  = async function (){
    const url = 'http://192.168.1.109:8085/register/login';
    const formData = new FormData();
    formData.append("usrname", "*******");
    formData.append("passwd", "********");

    const config = {
        headers: {
            'content-type': undefined
        },
        method: 'POST',
        //mode: 'cors', // if mode no-cors 
    };

    $http.post(url, formData, config)
        .then(function successCallback(response) {
            console.log(response);
            toastr.success('success');
        }, function errorCallback(response) {
            console.log(response);
            toastr.error('error');
        });
};
$scope.postLogin();


$scope.zwnetGetDesc = async function zwnetGetDesc(){
    await fetch('http://192.168.1.109:8085/cgi/zcgi/networks//zwnet_get_desc', {
        credentials: 'same-origin',
        headers: {
            'user-agent': 'Mozilla/4.0 MDN Example',
            'content-type': 'text/xml'
        },
        method: 'POST',
        mode: 'no-cors'
    })
        .then(blob => blob.text())
        .then(data => {
            console.log(data);
            return data;
        })
        .catch(error => {
            console.log(error);
            return error;
        });
};

$scope.zwnetGetDesc();

};

https://ibb.co/ePchEc [标题截图] https://ibb.co/jnQPSx [xml回复正文]

Gist Link:https://gist.github.com/umutyerebakmaz/073424a4195e75db482ac71e378e4408

1 个答案:

答案 0 :(得分:0)

问题有多个细节。我发现它有自己的才能,并且做得更好。

<强>的httpd.conf

Header always set Access-Control-Allow-Origin "http://192.168.1.109:8086"    
Header always set Allow-Access-Credential true // outside to if module label.

<强> device.controller.js

$scope.config = {
   withCredentials: true, // add all http.post 
   headers: { 'Content-Type': 'text/plain' } // The content-type parameter I wanted to send to the server did not overlap.
};

使用海盗船时,请求和响应标头必须完全相同。这条规则是导致失败的最重要原因。