Angular Method for sending cross site requests using $http. jsonp

  • 2021-08-05 08:03:53
  • OfStack

This article illustrates how Angular uses $http. jsonp to send cross-site requests. Share it for your reference, as follows:

In the practice of sending cross-site requests using $http. jsonp in Angular, one of the following issues was encountered:

1. Not all url that return json format support jsonp. The server side needs to support reading the return function from url and encapsulating json data with it.

2. In AngularJS v 1.6. 1, the parameter callback cannot be included in url, but is specified with jsonpCallbackParam

$http.jsonp('some/trusted/url', {jsonpCallbackParam: 'callback'})

3. Add url to the whitelist, otherwise you will encounter the error of browser homology policy restriction.


angular.module('mthtran')
.config(function($sceDelegateProvider) {
 $sceDelegateProvider.resourceUrlWhitelist([
  // Allow same origin resource loads.
  'self',
  // Allow loading from our assets domain.
  'http://query.yahooapis.com/v1/public/**'
 ]);
})

For more readers interested in AngularJS related content, please check the topics of this site: "Summary of AngularJS Instruction Operation Skills", "Introduction and Advanced Tutorial of AngularJS" and "Summary of AngularJS MVC Architecture"

I hope this article is helpful to everyone's AngularJS programming.


Related articles: