简体   繁体   English

AngularJS $ http返回404而不是200

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

相关问题 AngularJS http.get返回404 - AngularJS http.get returns 404 对于查询参数过滤的 GET 请求,json-server 返回 HTTP 200 而不是 404,这里的最佳做法是什么以及如何返回 404? - json-server returns HTTP 200 instead of 404 for query parameter filtered GET request, what is the best practice here & how can 404 be returned? Python Flask Web服务器未收到JSON Ajax POST请求,服务器返回HTTP 200 OPTIONS而不是201 POST - Python Flask webserver not receiving JSON ajax POST requests, server returns HTTP 200 OPTIONS instead of 201 POST AngularJS $ http返回200 ok,但失败了 - AngularJS $http returning 200 ok, but fail Angularjs $http 返回错误 0 - Angularjs $http returns error 0 jQuery-获取一个URL,该URL在前几次获取时返回404,然后返回200 - JQuery - Fetching a URL that returns 404 on first few fetches, then returns 200 HTTP请求返回200 OK,但没有响应内容 - HTTP Request returns 200 OK but no content in response Express JS路由器有时返回404,有时返回200 - Express JS router sometimes returns 404 and sometimes 200 AngularJS:$ routeProvider解析$ http时返回响应obj而不是我的obj - AngularJS: $routeProvider when resolve $http returns response obj instead of my obj AngularJS Mocking $ http返回错误 - AngularJS Mocking $http returns Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM