简体   繁体   English

提取API模式:先不分配分配,然后分配函数变量

[英]Fetch API mode: no-cors assign then function variable

Hello I created this code block, and i don't know how to assign then function variable. 您好,我创建了此代码块,但我不知道该如何分配函数变量。

Api Server: 192.168.1.109:8085 api服务器: 192.168.1.109 : 8085

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

For Cors all my settings seem to be correct. 对于Cors,我的所有设置似乎都是正确的。 I think :/. 我认为 :/。

The main responsibility is to validate and capture data when fetching data with fetch. 主要职责是在通过访存来访存数据时验证和捕获数据。 I can not print the response data from the screen even if I succeeded. 即使成功,我也无法从屏幕上打印响应数据。 Please save me . 请救救我。 I was stack, Today 9. day in my life :( 我当时是栈,今天9.我生命中的一天:(

httpd.conf 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>

Client Application codes for fetching data. 客户端应用程序代码用于获取数据。 device-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){ 函数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 [header screenshot] https://ibb.co/jnQPSx [xml response body] https://ibb.co/ePchEc [标题屏幕截图] https://ibb.co/jnQPSx [xml响应正文]

Gist Link: https://gist.github.com/umutyerebakmaz/073424a4195e75db482ac71e378e4408 要点链接: https : //gist.github.com/umutyerebakmaz/073424a4195e75db482ac71e378e4408

The problem had multiple details. 该问题有多个细节。 I found it with my own talents and made it better. 我凭自己的才华发现了它,并使其变得更好。

httpd.conf 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 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.
};

the request and response headers must be exactly the same when working with corsairs. 使用corsairs时,请求和响应标头必须完全相同。 this rule is the most important cause the failure of the slightest difference. 此规则是造成故障最重要的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在模式为“ no-cors”的情况下使用获取API获取数据? - How to get data using fetch API with mode 'no-cors'? 尝试使用 fetch 和 pass in 模式:no-cors - Trying to use fetch and pass in mode: no-cors 使用带有 fetch API 的“no-cors”模式时,请求标头未按预期设置 - Request header not set as expected when using 'no-cors' mode with fetch API `fetch`,与模式`no-cors`一起使用不会调用promise.then回调 - `fetch`, work with mode `no-cors` does not call the promise.then callback 使用模式为“ no-cors”的提取polyfill,响应状态= 0 - Response status = 0 using fetch polyfill with mode: 'no-cors' 使用 Fetch api DELETE 方法并启用 no-cors - Using Fetch api DELETE method with no-cors enabled 如何在 fetch() 中使用 no-cors - How use no-cors with fetch() 使用javascript提取,在no-cors,cors和same-origin模式之间有所不同 - Fetch in javascript, different between mode no-cors, cors and same-origin 当我以“ no-cors”模式获取资源时,body-parser为什么会失败? - Why does body-parser fail when I fetch the resource in 'no-cors' mode? 如果响应不透明,如何从 url 获取数据。 模式:“no-cors”没有显示任何效果 - How to fetch the data from the url if the response coming is opaque. mode: "no-cors" is not showing any effect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM