0

I'm trying to replace my $.ajax call with $http.get(). I get a 404 Not Found error when I try.

Here is the ajax call:

    //  ToDo: See if there is an $http.get equivalent. That way the callback doesn't have
    //        to be wrapped in $scope.apply().
    $.ajax({
        url: '/PrestoWeb/api/ping/responses/',
        type: 'POST',
        data: JSON.stringify(latestPingRequest),
        contentType: "application/json",
        success: function (responses) {
            // do stuff
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

And this is the $http call:

    var config = {
        url: '/PrestoWeb/api/ping/responses/',
        method: 'POST',
        data: JSON.stringify(latestPingRequest),
        contentType: "application/json"
    };

    $http.get(config)
        .then(function (response) {
            // do stuff
        }, function (response) {
            alert(response);
    });

The ajax call works. The http call does not. The URL, type, and data are the exact same in both calls. What am I missing?

19
  • 6
    You realize, $http.get with method: post is contradicting, right? Commented Dec 22, 2015 at 21:49
  • 2
    You are making a post in the original $.ajax call. Do you want to make a POST or a GET? If post, then you should use $http.post(config) Commented Dec 22, 2015 at 21:49
  • 4
    These downvoters are ridiculous. Why the downvotes? If it is because the OP didn't know to use $.post instead of $.get it is because he clearly presented his code and the problems he was having with it. Seems like a good question to me Commented Dec 22, 2015 at 21:53
  • 1
    $http.post() and $http.get() are shortcut methods. The first argument should be a URL. You don't even need the config object, because Angular will do those things for you: $http.post(url, data); Commented Dec 22, 2015 at 21:54
  • 1
    $http.post(url, data, config). Also the $http module automatically stringifies for you, so you don't need to be doing that yourself. edit yes as @SunilD. says, you likely just need $http.post(url, { some: 'data' }) Commented Dec 22, 2015 at 21:54

1 Answer 1

2

It looks to me like the problem you are having is using $http.get instead of $http.post. The $http object has some helper methods for the common http verbs, e.g. $http.get, $http.post, $http.put which will set the method to the name of the shorthand. The helper method for post expects three parameters, a url, your data and a configuration object so your call would look like, $http.post('/PrestoWeb/api/ping/responses/', latestPingRequest, config)

In your case you specify method: 'POST' in your http configuration object but then use the $http.get method, which will make a get request instead of what you specified in your configuration object.

Because you specified the method in your configuration object, you could just use $http(config) and skip the helper methods entirely. I actually prefer doing things this way as your full request is defined in the configuration object and not the configuration object and method used. The helper methods also all have different signatures which is confusing. Easier just to stick to the $http(config) IMO

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was helpful! I learned something, and that's what SO is supposed to be about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.