0

I have a webservice which returns Jsonobject and I call it in JS using Ajax like here :

$scope.init = function () {
                $.ajax({
                    type: 'GET',
                    timeout: 1000000000,
                    url: serverName.replace('services', 'servicesV2') + '/getTreasuryResults',
                    data: { data: 'None' }, // the data in form-encoded format, ie as it would appear on a querystring
                    dataType: "text", // the data type we want back, so text.  The data will come wrapped in xml
                    success: function (data) {
                        var dataAngularTreasury = JSON.parse(data);
                        alert(dataAngularTreasury);
                        //FillDefaultTreasuryValues(data.split(";"));
                        CallTreasuryDetailValuesWS(934);
                    },
                    error: function (data) {
                        alert(errServiceCall); // show the string that was returned, this will be the data inside the xml wrapper
                        $('#loading').hide();
                    }
                });
            };

If I call that init() function in ng-click like

<button id="btnGetDefaultValues" type="button" class="button" ng-click="init()">Fill</button>

It runs with no problem.

but if I call this webservice in page load like

angular.module('termDeposite', []).controller('userCtrl', function ($scope) {

    $scope.treasuryValues = [];


    angular.element(document).ready(function () {
        $.ajax({
            type: 'GET',
            timeout: 1000000000,
            url: serverName.replace('services', 'servicesV2') + '/getTreasuryResults',
            data: { data: 'None' }, // the data in form-encoded format, ie as it would appear on a querystring
            dataType: "text", // the data type we want back, so text.  The data will come wrapped in xml
            success: function (data) {
                var dataAngularTreasury = JSON.parse(data);
                alert(dataAngularTreasury);
                //FillDefaultTreasuryValues(data.split(";"));
                CallTreasuryDetailValuesWS(934);
            },
            error: function (data) {
                alert(errServiceCall); // show the string that was returned, this will be the data inside the xml wrapper
                $('#loading').hide();
            }
        });
    });
});

or

if I call this webservice in ng-init trigger like

<body id="body" ng-app="termDeposite" ng-controller="userCtrl" ng-init="init()"  >

webservice goes to error step and throws that error :

"\n\n\nError 404--Not Found\n\n\n\n\n\n

Error 404--Not Found

\n\n\n

From RFC 2068 Hypertext Transfer Protocol -- HTTP/1.1:

\n10.4.5 404 Not Found\n

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent.

If the server does not wish to make this information available to the client, the status code 403 (Forbidden) can be used instead. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address.

\n\n\n\n\n\n"

Finally, I can call a webservice with using ng-click but I can't call same webservice using ng-init or pageload. So how can I call a webservice using ajax in page init or page load in angular framework scope ?

4
  • check using your browsers network tab or fiddler. are they making the exact same request? Commented May 12, 2015 at 13:47
  • Yeah I can call same webservice using copy full ws url and paste it to the browser tab directly, and it returns no error. Commented May 12, 2015 at 13:50
  • using .ready in controller does not make much sense Commented May 12, 2015 at 13:58
  • but i need that ws returned data in page load @YOU Commented May 12, 2015 at 14:03

1 Answer 1

2

Assuming you have serverName as a global and it is readable then the ng-init version or creating a custom directive and sticking the code in the link function should work.

Check the URL that is actually being called in the network tab in the Chrome dev tools (built into chrome) - cmd+alt+j on mac f12 on PC.

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

1 Comment

it's about serverName. Thank you @alexrogins

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.