我有一个AngularFullStack项目,我尝试使用下面的代码打我的项目的服务器端点,但问题是呼叫是不是从角度客户端到nodejs服务器。因为这是我的第一个项目,所以我很少不知道可能会导致这个奇怪冲突的问题。客户端无法击中其自己的服务器端点
客户方:
$http({
method: 'POST',
url: '/api/soapAPIs/soapAPI',
data: request,
headers: {
'Content-Type': 'application/json'
}
})
.then(function(response) {
console.log("response from server is: ", response);
})
.catch(function(error) {
console.log("error in calling server is: ", error);
});
我已经安装在服务器端CORS并写在app.js下面的代码,但仍然不起作用。
服务器App.js
// Setup server
var app = express();
var cors = require('cors');
//add cors to do the cross site requests
app.use(cors());
服务器端点代码:
export function soapAPI(req, res) {
console.log("SERVER SIDE ", req.body);
res.status(statusCode).json({"status":"success"});
res.send();
}
以下是问题:
如果我尝试打端点的NodeJS像这
/api/soapAPIs/soapAPI
,浏览器显示pending
请求,它永远不会去服务器。当我为端点添加完整分类网址(如
http:localhost:9000/api/soapAPIs/soapAPI
)时,它会击中端点,但客户端永远不会收到来自服务器的响应。我不想在客户端提供完整的分类网址,原因很明显。
如何解决此问题?如果您需要其他代码/信息,请告诉我。
编辑:
用完全分类路径,服务器端点被击中,但它的响应永远不会被客户端接收。我得到了浏览器这个错误:
Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"/api/things","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}
请分享server.js的完整代码(GIST,github上,..) –
@BadisMerabet你去那里:https://gist.github.com/wingoku/38a77a2086441a8a752e77373694a938 –