0

I have created a Web API that is working from the address line of my browser, but when I try to write AngularJS or jQuery calls they fail with no explanation. I think I don't have the correct route in the URL, or maybe it's a JSON formatting problem. I had an earlier version of the Web API and the same calls from AngularJS or jQuery worked. But in that earlier version I had declared the action in the route. Then I read in a RESTful Web API it's best practice to omit the action in the route and base the route off the HTTP method. So I rebuilt the Web API to do it that way, but now I can't reconnect on the client side. Can someone see the error in my URL route? You can test the URL in this example and see the data in your browser.

Note: I think I have solved this problem. I need to add a JSON formatter in my new Web API. It was in my first version. That is the only difference I have found so far.

            var sURL2 = "http://stevegaines.info/api/Securities?SecurityType=CASH";
            $http.get(sURL2)
                .then(function (oData)
                {
                    alert("oData = " + oData);
                    $scope.Securities = oData;
                }, function (response)
                {
                    alert("error = " + response.statusText);
                });

3
  • i think what $http should send a object, i always use $http like a : var req = { method: 'GET', url: 'your url' }; $http(req).then( function successCallback(successData){}, function(errorCallback(errorData){}); hm, show source-cod of your controller and model. and yes, check json formatter. Commented Apr 27, 2016 at 5:06
  • Just add your endpoint api adress to the $http.get() and not the whole url, use: var sURL2 = "api/Securities?SecurityType=CASH"; Commented Apr 27, 2016 at 5:26
  • what is the route defined in the routeconfig? Commented Apr 27, 2016 at 6:47

1 Answer 1

1

I finally got this to work. I think the problem lay in the client javascript, but I also re-wrote the Web API, so I'm not sure. One thing I know changed in the javascript was I had used the jQuery .success method, but I changed it to .then because of jQuery deprecating .success. When I did that I had to use the data property of the dataResponse. Just plain dataResponse won't work. I guess it's a different type of object.

            $http.get(sURL2)
                .then(function (dataResponse)
                {
                    // this doesn't work
                    // $scope.Securities = dataResponse.data.records;
                    // this doesn't work
                    // $scope.Securities = dataResponse;

                    // this works
                    $scope.Securities = dataResponse.data;
                }, function (response)
                {
                    alert("error = " + response.statusText);
                });

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

Comments

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.