Note that there are some explanatory texts on larger screens.

plurals
  1. POAngularJS JSONP issue
    primarykey
    data
    text
    <p>I am able to get data with</p> <pre><code> $http.jsonp("http://url/GetAll?callback=JSON_CALLBACK"). success(function(data, status, headers, config) { console.log(JSON.stringify(data)); $scope.eventSource = JSON.stringify(data); }). error(function(data, status, headers, config) { alert("ERROR: Could not get data."); }); </code></pre> <p>I know I can also use a callback method to get data like this:</p> <pre><code>$.ajax({ url: "http://url/GetAll", type: 'GET', dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'myCallback' }); myCallback = function (data) { alert(data); //$scope.eventSource = JSON.stringify(data); } </code></pre> <p>Unfortunately I need this as a factory or a service and will be called inside a module. So what I tried is doing this, but it does not work:</p> <pre><code>app.factory('calendarFactory', function ($http) { return { query: function () { var promise = $http.jsonp("http://url/GetAll?callback=JSON_CALLBACK"). success(function (data, status) { //console.log(JSON.stringify(data)); //myData = JSON.stringify(data); return JSON.stringify(data); }). error(function (data, status, headers, config) { alert("ERROR: Could not get data."); }); return promise; } } </code></pre> <p>});</p> <p>The console prints the data.. but when I try to add it to a scoped variable, it does not work..</p> <pre><code> $scope.eventSource = calendarFactory.query(); </code></pre> <p>Script references are all good because it works with dummy data. Please don't say a callback method is required. Look at my second script. That has a callback method. I need the third script to work. Something's definitely wrong as I am not getting data in the fourth script. </p> <p><strong>More Update:</strong></p> <p>I changed the controller to this:</p> <pre><code> $scope.eventSource = []; calendarFactory.query().success(function (data) { //myVar = JSON.stringify(data); console.log(JSON.stringify(data)); $scope.eventSource = JSON.stringify(data); }); console.log($scope.eventSource); </code></pre> <p>Now while this is more promising, the problem is the log displays [] first and then the data. This means the factory call is happening very late. Too late for the changes to stick. I tried with a <em>$scope.$watch('eventSource', function () { same factory call}</em> but that does not work either.</p> <p>Now, I am really desperate to get this working. I can consider even a custom JQuery function that can do the trick. </p> <p><strong>Some more updates..</strong> So I now tried using promise..</p> <pre><code>app.service('calendarFactory', function ($http, $q) { return { query: function () { var deferred = $q.defer(); $http({ method: "jsonp", url: URL }) .success(function (data, status, headers, config) { deferred.resolve(data); //console.log(JSON.stringify(data)); }).error(function (data, status, headers, config) { deferred.reject(data); }); return deferred.promise; } } </code></pre> <p>});</p> <pre><code>$scope.eventSource = calendarFactory.query(); $scope.eventSource.then(function (items) { $scope.eventSource = JSON.stringify(items); //console.log(JSON.stringify(items)); }, function (status) { console.log(status); }); </code></pre> <p>But it still does not update the page.</p> <p>Can someone show a working sample of an angular service that makes JSONp request?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload