Avoid using this unauthorized JSONP API - Ways to retrieve information without using the CALLBACK parameter

Dealing with Angular 1.6 Error when Using JSONP despite Receiving a 200 Ok Response from URL

I encountered an issue where I need to fetch some data from a JSONP endpoint, and although the response seems to contain the expected data, Angular is still throwing an error.

var url = "https://careers.icims.com/jobs-api/"

var trustedUrl = $sce.trustAsResourceUrl(url);

$http.jsonp(trustedUrl, {jsonpCallbackParam: 'jobs'}).then(function(res){

    console.log(res); // unfortunately, this line never gets executed

});

The error message that I am seeing is:

Uncaught ReferenceError: jobs is not defined
    at jobs-api?jobs=angular.callbacks._0:1
where jobs refers to my JSONP callback prefix.

However, if we inspect the response closely, we can see that it includes the JSONP script: https://i.sstatic.net/pR1ci.jpg

Can anyone shed light on why this exception is happening and suggest a solution to overcome it? My current Angular version is 1.6.0.

Answer №1

Warning: Unauthorized JSONP API

The API located at that URL does not comply with JSONP standards.

It can only be accessed through a risky service:

app.service("unsafeAPI", function($q) {
  this.get = get;
  
  function get(funcName, url) {
    var dataDefer = $q.defer();
  
    window[funcName] = function(x) {
      dataDefer.resolve(x);
    }

    var tag = document.createElement("script");
    tag.src = url;

    document.getElementsByTagName("head")[0].appendChild(tag);
    
    return dataDefer.promise;
  }
})

Proceed with caution when using this API.

View the Live DEMO

angular.module("app",[])
.service("unsafeAPI", function($q) {
  this.get = get;
  
  function get(funcName, url) {
    var dataDefer = $q.defer();
  
    window[funcName] = function(x) {
      dataDefer.resolve(x);
    }

    var tag = document.createElement("script");
    tag.src = url;

    document.getElementsByTagName("head")[0].appendChild(tag);
    
    return dataDefer.promise;
  }
})

.run(function($rootScope, unsafeAPI) {
    var url = "https://careers.icims.com/jobs-api/";
    unsafeAPI.get('jobs',url).then(function(data) {
      $rootScope.data = data;
    })
})
<script src="//unpkg.com/angular/angular.js"></script>
  <body ng-app="app">
    <h1>Unsafe API Demo</h1>
    <pre>{{data | json}}</pre>
  </body>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Steps for releasing a third-party library that is compatible with both Angular 2 and Angular 4

Currently, I am utilizing a third-party library for Angular that is compatible with Angular 2. However, I want to make sure this library can support all versions of Angular, including Angular 4 and Angular 5. How can I go about publishing an updated vers ...

My program is throwing an error due to invalid JSON format

Snippet of code: var data = $.parseJSON(data); Error message: Encountered Invalid JSON: {times: [{'9:30am','10:00am','10:30am','11:00am','11:30am','12:00pm','12:30pm','1:00pm&apo ...

Tips for retrieving sent data from a jQuery Ajax request on XHR error

I am facing a situation where I have numerous jQuery AJAX requests being sent using a single function. function sendData(data){ $.post("somepage", {"data": data}, function() {}, "json") .fail(function(jqXHR, textStatus, errorThrown){ sendD ...

Restricting JSON output using date and time values within the JSON object

Currently, I'm in the process of creating a play data reporting tool specifically designed for a radio station. The main concept involves retrieving play history from the playout software's API and manually adding tracks that were played outside ...

The jQuery function may encounter issues when trying to target elements within an appended HTML form

I am encountering a couple of issues. I have 2 separate JQuery functions, one for appending content and the other for displaying the selected file's name. FUNCTION 1 <script> jQuery( document ).ready(function( $ ) { $("input[name ...

Exporting SSRS reports in JSON format is a breeze

We are currently using SQL Server Management Studio to create a stored procedure that generates JSON output using "FOR JSON PATH." Our challenge is finding a way to use SSRS to run this stored procedure and export the data as a .json file. Is there a met ...

I am on the lookout for a spell-checking tool that can seamlessly integrate with both Django and Python 2.7

Is there a way to detect spelling errors in real-time as the user types, with support for multiple languages? Your help would be greatly appreciated. Thanks! ...

Angular HttpClient not recognizing hashtag

I'm trying to make a REST API call, but running into issues with the developerId parameter being sent incorrectly: let developerId = "123#212"; let url = \`\${Constants.BASE_URL}\${marketId}/developers/\${developerId}\`; retur ...

Utilizing ng-model on a pair of inputs to achieve synchronized values

Is it possible to use ng-model on an input field that receives a value from another input field? I'm having issues with my code and I can't figure out why. Can ng-model be set to an input field using Angular values? Here's my code: ...

What is the best way to use jest/enzyme to determine if a method from an imported class was called in a component

Here is the code snippet from my component: export class VehiclesComponent extends React.Component { constructor(props) { super(props); this.state = { data: [], }; autoBind(this); } componentDidMount () { this.fetchData(); ...

What are the steps for implementing secure login and authorization process in Oracle's RESTful web services?

Currently, I am utilizing Oracle RESTful Web Services within Oracle APEX to retrieve data from the Oracle database. While I am familiar with fetching data using Oracle web services, I am struggling to determine how to properly send a username and password ...

What is the recommended TypeScript type for setting React children?

My current layout is as follows: export default function Layout(children:any) { return ( <div className={`${styles.FixedBody} bg-gray-200`}> <main className={styles.FixedMain}> <LoginButton /> { children } ...

The duration of user engagement on a website with an embedded iframe

I am working on measuring the time users spend on web pages, excluding the time they navigate away from the browser. While exploring open source libraries like Timejs, I noticed that these tools do not accurately track user time when they are watching vid ...

Unable to input characters consecutively into the text field because it is being displayed as a distinct function within the component

When attempting to bind a text field using the approach below, I encounter an issue where entering characters in the text field causes the control/focus to shift out of the field after the first character is entered. I then have to click back into the text ...

What exactly occurs when a "variable is declared but its value is never read" situation arises?

I encountered the same warning multiple times while implementing this particular pattern. function test() { let value: number = 0 // The warning occurs at this line: value is declared but its value is never read value = 2 return false } My curi ...

Using Typescript to set a variable's value from an asynchronous function

Could someone help me with a challenge I am facing while using Typescript? I am attempting to assign the return value from an async service call to a local variable like this: private searchResult; public search():void{ this.DashboardServi ...

Adding color dynamically to text within ion-card based on a regex pattern

My goal is to enhance the appearance of certain text elements by wrapping them in a span tag whenever a # or a @ symbol is detected, creating the look of usernames and hashtags on Twitter. Below is the code I am currently using: TS FILE: ngOnInit(): void ...

How to output a string in jQuery if it includes a certain character?

I need to extract all strings that include a specific word within them, like this: <div> <div> <span> <h3>In Paris (Mark Bartels) worked for about 50 years.</h3> </span> </div> ...

Tips for Serializing and Deserializing an XML Document using Javascript

I keep encountering the error 'parameter1 is not a valid node' when using document.adoptNode. Here's the code snippet in question: $.ajax({ type: "GET", url: url, datatype: "xml", success: function (results) { ...

There are various buttons available for users to choose from on a page. How can I retrieve this selection data in JSON format?

I am designing a quiz with HTML buttons as options. How can I capture the user-selected button data before they click 'next' and format it into JSON? As there are multiple pages of questions, the data needs to be dynamically added to the JSON unt ...