简体   繁体   中英

Server blocks POST but not GET requests?

Inside my app, I use $http post requests because on server side all PHP services accept only post request. PHP web services are located in godaddy.

After some consecutive $http post requests(usually after 10-20 request), I start to get ERR_EMPTY_RESPONSE from server. I did some research and found that this is because a security issue from server to prevent attacks. Apache server's mod_sec setting causes this problem but I don't have permissions to override it.

I decided to test, if this issue is specifically caused by POST requests. so I wrote a simple PHP file and AngularJS test script. If I use GET request I don't get any ERR_EMPTY_RESPONSE errors. Are there any $http POST settings on client side AngularJS or any Apache server setting which I can place in .htaccess file, so server doesn't block POST requests?

PHP script:

<?php 
       echo json_encode(array("item"=>"dummy test service."));
 ?>

AngularJS script:

var success_count = 0;
var method = 'GET'; //try 'POST' and you will get ERR_EMPTY_RESPONSE
stop = $interval(function() {
    $http({
        method : method,
        url : 'test.php'
    }).then(function successCallback(response) {
        console.log(success_count);
        success_count++;
    }, function errorCallback(response) {
        console.log("error after "+success_count+" successful request.");
        success_count = 0;
    });
}, 200);

This is because of cross-domain request policy. If you want to restrict scripts from other pages that are not on the same domain this will help in doing that. But what happens in the case of API's, how are they giving permission to accept request from some other pages?. So to do this they use Access-control-OPTIONS and in your particular case Access-Control-Allow-Methods must have been set to just GET method. So its basically accepting only GET request.

However if you want to specifically use POST request use CURL(with PHP or some other server side language) which can by pass same-origin policy ,and can send & receive data through POST request. You can have additional information about CURL here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM