角度:$ http请求给出-1状态 [英] Angular: $http requests gives -1 status
问题描述
node.js服务器给出这是一个字符串". (writeHeader,write(字符串),结束).
A node.js server gives "This is a string". (writeHeader, write( string), end).
在执行$ http请求时,我看到node.js服务器正在响应并将信息发送回去.
When performing a $http request, I see that the node.js server is responding and sending the information back.
在Angular中,我执行以下请求:
In Angular I perform the following request:
angular.module('phoneList').
component('phoneList', {
templateUrl: 'phone-list/phone-list.template.html',
controller: function PhoneListController($http) {
var self = this;
$http.get('http://127.0.0.1:8081/echo').then(function(response) {
self.data = response.data;
}, function(err) {
self.error = err;
self.status = response.status;
self.statusText = response.statusText;
});
}
});
回复
{数据":null,状态":-1,配置":{方法":"GET","transformRequest":[null],"transformResponse":[null],"url": " http://127.0.0.1:8081/echo ",标题":{接受": "application/json, text/plain,/}}," statusText:"}
{"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"url":"http://127.0.0.1:8081/echo","headers":{"Accept":"application/json, text/plain, /"}},"statusText":""}
我都尝试过仅从node.js或HTML文本发送JSON.没有不同.
I tried both just sending JSON from node.js or HTML-text. No difference.
推荐答案
非常感谢, @Sebastian Sebald 和 @Dex 指导我提出了解决方案.我只是想在我的计算机上运行一个简单的Node.js服务器,将消息发送到我的(简单)Angular脚本.
Thanks a lot, @Sebastian Sebald and @Dex for guiding me to the solution. I just wanted to run a simple Node.js server on my computer serving messages to my (simple) Angular script.
是的,这是一个跨域问题. @TonyTakeshi 为该问题提供了很好的解决方案.您可以通过以下方法在node.js服务器文件中解决该问题:
Yes, it was a cross-domain issue. @TonyTakeshi gave a good solution to this issue. You can solve it in the node.js server file via:
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
用于简单测试配置的大量内容;-)
Heavy stuff for a simple test configuration ;-)
这篇关于角度:$ http请求给出-1状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!