-2

I'am working on angularjs project in which there are 2 users admin and office staff.And to insert data to database i need to send data to server using http request using post method and this request is received on server side in php. This is simple request to server with some parameters and its working correctly until both the users hit submit button and http request is made at the same time. ie when http request is made by both the users at the same time.If that happens than 2nd request is does not get any response.Now my question is that is there a way to check if server is processing one http request and make 2nd request wait.

angularjs:

$http({
    method: "post",
    url: $localStorage.weburl + "abc.php?f=abc_data",
    timeout: 1 * 10 * 2000,
    params: {
        action: "add"
    },
    data: {
        data1: data1,
        data2: data2
    }.then(function mySucces(response) {

    }, function myError(err) {
        $ionicLoading.hide();
        alert("Please check the connection");
    })
});

PHP:

function abc_data($conn)
{
    $_POST = json_decode(file_get_contents('php://input'), true);
}   
JustCarty
  • 3,261
  • 3
  • 26
  • 46
Nisha
  • 148
  • 14
  • Both users are using same HTTP? – Mayank Vadiya Feb 15 '19 at 10:16
  • Yes sir.both user are using same HTTP – Nisha Feb 15 '19 at 10:20
  • Its works perfect when both user call server at different time.issue is only when both call at same time – Nisha Feb 15 '19 at 10:22
  • You should look at https://stackoverflow.com/questions/33244752/how-to-set-a-timeout-to-abort-an-http-get-inside-a-factory-or-service – Mayank Vadiya Feb 15 '19 at 10:29
  • 3
    There is no such thing as calling at the same time. What do you mean by same time? a same milisecond? What php version do you use as php5.6 has different stream read behavior. Also explain why are you using stream to get data? – vytsci Feb 15 '19 at 10:29
  • @Nisha do not add any timeout, because this issue has something to do with php://input, because in normal cases such requests will be handled without any problems at all, but I believe this is due to restriction mentioned in official docs https://secure.php.net/manual/en/wrappers.php.php this issue may occur because first requests is accessing stream and after it closes it another request loses it and results in an error. I suggest you to enable error reporting. – vytsci Feb 15 '19 at 10:32
  • @vytsci is right its not possible to call same time because there is some into milliseconds, or if your first request not respond and second request is executed then you should use `$q` – Mayank Vadiya Feb 15 '19 at 10:33
  • My suggestion is that php://input is becoming inaccessible after first request. How this happens I have no idea, but I dont think you need php://input at all, just use $_POST global variable. – vytsci Feb 15 '19 at 10:38
  • @vytsci sir http request is basically to insert data into database if i don't use timeout and it is not connected to server than same data is inserted multiple time. – Nisha Feb 15 '19 at 10:39
  • And how can i enable error reporting ? – Nisha Feb 15 '19 at 10:40
  • @Nisha you could easily have found here on SO how to enable error reporting... https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – Shadow Feb 15 '19 at 10:43
  • @vytsci That is due to AngularJS being weird: https://stackoverflow.com/a/15485690/3578036 – JustCarty Feb 15 '19 at 11:13
  • It sounds like a blockage. Some details of your server environment would help. Which database backend? How are you running your Php server? Do you get similar results when sending standard http requests, with mocked data? – Progrock Feb 15 '19 at 11:57

1 Answers1

0

You should try like this way

var deferred = $q.defer();
$http.post($localStorage.weburl + "abc.php",{action: "add"})
    .success(function (data) {
        deferred.resolve(data);
    })
    .error(function (error) {
        deferred.reject(error);
    });

return deferred.promise;

If you are using post method then pass you data like {data:arrayOfdata} instead of url: $localStorage.weburl + "abc.php?f=abc_data"

Mayank Vadiya
  • 1,373
  • 2
  • 18
  • 30
  • $localStorage.weburl + "abc.php?f=abc_data":Here abc_data is the name of function inside abc.php. Where different insert and select queries are placed – Nisha Feb 15 '19 at 10:45
  • Oh dear, you are using the GET data to call functions?!?! That is a huge security flaw. What if somewhere were to change `f=get_user` and pass in `data: { 'user': 1 }` or something? Dynamic function calling sounds very dangerous... – JustCarty Feb 15 '19 at 11:15
  • @JustCarty you are right, Now i have no anymore solution for this scenario – Mayank Vadiya Feb 15 '19 at 11:30
  • Apologies, that was aimed at @Nisha – JustCarty Feb 15 '19 at 12:09
  • Just call on abc.php and pass some identifier to run particular function – Mayank Vadiya Feb 15 '19 at 12:21