[英]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.