[英]Angularjs $http returns a 404 instead of a 200
I am currently trying to upload some data to my self-written backend using the angularjs $http object. 我目前正在尝试使用angularjs $ http对象将一些数据上传到我的自写后端。 Using this object I have some weird behavior in my coding. 使用此对象,我的编码中有些奇怪的行为。 When I run my application using the emulator from the Intel XDK IDE I have no problems. 当我使用来自Intel XDK IDE的仿真器运行应用程序时,我没有任何问题。 When I run the application on my real Smartphone my $http calls make some problems. 当我在真正的智能手机上运行该应用程序时,我的$ http调用出现了一些问题。
I debugged my application and in the network overview my calls return a responsecode 200. Anyways the .error(...) handling is performed and the .success(...) handling is skipped. 我调试了我的应用程序,并在网络概述中,我的调用返回了响应代码200。无论如何,将执行.error(...)处理,并跳过.success(...)处理。
My coding looks like this: 我的编码如下:
$http({
method: 'POST',
url: dbServerPath + "SetStatUser.php",
data: exporting
}).success(function (response) {
console.log(response);
successSave();
}).error(function (response, code) {
console.log(response);
console.log(code);
errorSave();
});
it looks like it returns the error even if the call is not finished. 即使调用未完成,它看起来仍会返回错误。
The problem must be at the frontend. 问题一定在前端。 Because I have tested my backend using Postman. 因为我已经使用Postman测试了后端。
Edit: For further clarification, my backend returns nothing, it just processes the data given with the request payload. 编辑:为进一步说明,我的后端什么也不返回,它只处理请求有效负载给出的数据。 Firefox and Google Chrome track the response with the http code 200 but the angularjs object $http tracks it as a 404. So my by backend can not be the trigger for this weird behavior. Firefox和Google Chrome使用http代码200跟踪响应,但是angularjs对象$ http将其跟踪为404。因此,后端发出的my 不能触发此怪异行为。
Try to use .then and .catch
, might be your successSave()
method throwing error and it never caught. 尝试使用.then and .catch
,可能是您的successSave()
方法抛出错误, successSave()
未被捕获。
$http({
method: 'POST',
url: dbServerPath + "SetStatUser.php",
data: exporting
}).then(function (response) {
console.log(response);
successSave();
}, function (error) {
console.log(error);
errorSave();
}).catch(function(err){
console.log(err);
});
I found the solution regarding my problem. 我找到了有关我的问题的解决方案。
Each request needs a kind of response, even if it just null or {} like in this question: Ajax request returns 200 OK, but an error event is fired instead of success I just added a 每个请求都需要一种响应,即使它只是null或{},例如这个问题: Ajax请求返回200 OK,但是会触发一个错误事件,而不是成功,我只是添加了一个
echo "null";
to my PHP backend. 到我的PHP后端。 I hope this will help if someone have the same problem as I had. 如果有人遇到与我相同的问题,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.