javascript - Server blocks POST but not GET requests?

671

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);
17

Answer

Solution:

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

People are also looking for solutions to the problem: php - Angular JS + $http post does not actually post the data

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.