0

I'm trying to sends some data along with a post to a specific url which has a php script behind it. At this moment I don't have access to the php script. The php script checks if the string matches any record in the db and returns that record if there is a match. If there is no match the script will return all the records.

The following code is what I have so far. As you can see I have a string named: shouldnotfindanyresultsstring. This should actually not return any results. However it returns all records instead of no records.

What I tried:

  • Use params instead of data
  • use different Content-types
  • use the short version of the post method

$http({
    url: $scope.url,
    method: "POST",
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    data: {search: "shouldnotfindanyresultsstring"}
}).then(function (response) {
    console.log(response);
}, function (response) { // optional
    console.log("Still not working");
});

So eventually I want to search records in the db with the search string. However I do not get it working.

With postman I can generate a post which works. I do have a strong feeling that it has something to do with the Content-type

Marc Dix
  • 1,864
  • 15
  • 29
RSSD
  • 63
  • 1
  • 13
  • 1
    Can you post the HTTP request that is done by Postman? – str Jun 01 '16 at 07:32
  • Getting data from the server without altering any should be a GET method. It isn't relevant to your question but I thought you should know this if you didn't. – Thijs Jun 01 '16 at 07:34
  • @str Not sure if this is want you mean but I add a `key`: search and a `value`: stringwithnoresults. In postman this gives me the result `null`. In the app this returns me all of the records – RSSD Jun 01 '16 at 07:41
  • @Thijs Yes I know this but I need to search with a string for some records – RSSD Jun 01 '16 at 07:42
  • @RSSD That is ambiguous. Please click "Generate Code" in Postman for your request and post the HTTP request. – str Jun 01 '16 at 07:46

3 Answers3

0

Try this:

$http.post($scope.url, JSON.stringify("shouldnotfindanyresultsstring"), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })

or this:

 $http.post($scope.url, JSON.stringify({search: "shouldnotfindanyresultsstring"}), { headers: {'Content-Type': 'application/x-www-form-urlencoded'} })
Roman Koliada
  • 4,286
  • 2
  • 30
  • 59
0

If you want to use x-www-form-urlencoded you need to actually encode the data as a string. Angular always posts your object as a JSON encoded object in your body, even if you specify that header.

It's explained in this answer and provides the solution

Community
  • 1
  • 1
ed'
  • 1,815
  • 16
  • 30
0

If you want to use 'application/x-www-form-urlencoded' then format your data as a string

data: "search=shouldnotfindanyresultsstring"

If you want to use 'application/json' then use this:

var jsonData = { search : "shouldnotfindanyresultsstring" };

$http({
    method: 'POST',
    url: $scope.url,
    contentType: 'application/json',
    data: JSON.stringify(jsonData),
}).
    success(function (data) {
        console.log(data);
    }).
    error(function (message, status) {
        console.log(message);
    });
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69