0

I am getting the below error when using a http get method.

:63502/Scripts/angular.js:14525 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","params":{"id":"23"},"url":"api/Project/GetProject","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}

HTML code,

<div class="row pb-20px text-center">
                <label class="col-sm-2 lb-date" for="projid">Project ID</label>
                <input type="text" id="projid" class="col-sm-2" ng-model="project.id" />
                <div style="padding-left:10px;">
                    <button type="submit" class="btn btn-primary col-sm-2" ng-click="ProjSearch()">Submit</button>
                </div>
                </div>

Angular code,

var myView = angular.module('myView', []);
myView.controller('ViewCtrl', ['$scope', '$http', '$filter', function ($scope, $http, $filter) {
$scope.ProjSearch = function () {
    debugger;
    t = {
        'id': $scope.project.id
    };
    var r = $http.get('api/Project/GetProject', { params: t })
            .then(function (response) {
                result = response.data;

                var c;

                $scope.project.title = result.ProjectTitle;

Please suggest on how to clear the error.

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Aneez
  • 61
  • 3
  • 13
  • http://stackoverflow.com/a/41993170/796400 looks similar to your issue – Raghu Venmarathoor May 10 '17 at 09:17
  • I am getting this error now – Aneez May 10 '17 at 09:23
  • :63502/Scripts/angular.js:12587 GET http://localhost:63502/GB/api/Project/GetProject?id=23 – Aneez May 10 '17 at 09:23
  • what error? I couldn't understand – Raghu Venmarathoor May 10 '17 at 09:24
  • the api I have provided is '/api/Project/GetProject' but it is throwing an error like '/GB/api/Project/GetProject' not found. not sure where the GB came from. It is just the controllername I have for my view and 'Project' is the controller name I have for the api – Aneez May 10 '17 at 09:29
  • The most likely cause of a `status: -1` error is a CORS problem. The browser is blocking the request because it violates [Same Origin Policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy) and the server has not authorized [Cross-Origin-Resource-Sharing (CORS)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS). – georgeawg May 11 '17 at 00:45

2 Answers2

1

In the $http.get() function, the first parameter handles the successful resolution of the promise and the second parameter to the function handles the error when the promise is rejected.

According to the documentation,

$qProvider handles whether to generate an error when a rejected promise is not handled. This feature is enabled by default.

https://docs.angularjs.org/api/ng/provider/$qProvider

Try providing a blank implementation of a function to handle the error.

    $http.get('api/Project/GetProject', { params: t })
        .then(function (response) {
             result = response.data;
             var c;
             $scope.project.title = result.ProjectTitle;
            },function(){
         });

Or just override the value of the errorOnUnhandledRejections to false in the config of the app.

app.config(['$qProvider', function ($qProvider) {
  $qProvider.errorOnUnhandledRejections(false);
}]);
Manas
  • 808
  • 1
  • 7
  • 16
-1

It seems to me the problem is this line:

result = response.data;

Are you sure your response object has a data field in it?

Mekicha
  • 811
  • 9
  • 21
  • I have a breakpoint at the api but that breakpoint itself is not being reached. response object has data field – Aneez May 10 '17 at 09:21