An error occurred while using $http.jsonp: unexpected character '<'

Even though I am receiving a response from the request, the .then() section of my code does not seem to be triggering. Can you help me figure out what mistake I might be making?

window.distance24Result = function(data){ alert(data.distance); };
$http.jsonp("http://www.distance24.org/route.jsonp?stops="+city+"|"+country,{data:'jsonp',
jsonpCallbackParam: 'distance24Result'
})
.then(function(data){console.log(data);});

Answer №1

If you take a look at the documentation, you will need to specify jsonp as the parameter for jsonpCallbackParam in order for Angular to use the correct request URL. This refers to the name of the GET parameter, not the callback function. Additionally, make sure to use route.json instead of route.jsonp.

$http.jsonp("http://www.distance24.org/route.json?stops="+city+"|"+country, {
    jsonpCallbackParam: 'jsonp'
}).then(function(data) {
    console.log(data);
    alert(data.distance);
});

Another option is to do the following:

$http.jsonp("http://www.distance24.org/route.json", {
    params: {stops: city+"|"+country},
    jsonpCallbackParam: 'jsonp'
})

Prior to Angular v1.6, the jsonpCallbackParam config value did not exist. Instead, you would utilize:

$http.jsonp("http://www.distance24.org/route.json?jsonp=JSON_CALLBACK&stops="+city+"|"+country)

or

$http.jsonp("http://www.distance24.org/route.json", {params: {
    "stops": city+"|"+country,
    "jsonp": "JSON_CALLBACK"
}})

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

Achieving dynamic key assignment when updating with mongoose in NodeJS and Express

With a multitude of keys requiring updates from a single function, I am seeking guidance on how to dynamically set the key for updating. static async updateProfile(req, res, next) { const userId = req.body.userId; // The key requiring an update ...

Clicking will cause my background content to blur

Is there a way to implement a menu button that, when clicked, reveals a menu with blurred content in the background? And when clicked again, the content returns to normal? Here's the current HTML structure: <div class="menu"> <div class=" ...

The URL functions properly in Postman, but encounters errors when used in Node.js

I've encountered an issue with a URL that functions properly in POSTMAN. However, when the same code is used in Node.js, it fails to work: const fetch = require('node-fetch'), express = require('express'), app = expre ...

Encountering a SonarQube error message stating "Unnecessary 'undefined' should be removed" when using "undefined" as a function argument

When calling a function, I have been passing "undefined" multiple times as a parameter. However, the SonarQube report is flagging an error stating "Remove this redundant undefined". https://i.stack.imgur.com/YhOZW.png It should be noted that the function ...

Calculating the distance of a child element from the top-left corner of its parent element in JavaScript

Can you determine the offset of a ChildElement in relation to a specific parent element? For instance, if a DIV is nested inside a parent DIV with the ID "xyz" Is there a method to achieve this using jQuery? let parent = $("#xyz"); let child = $("#abc") ...

Tips for utilizing vulnerable web scripts on SSL-enabled pages

After implementing SSL to secure my pages, I encountered an issue where one of my scripts no longer functions properly. Specifically, I had a script on my page that was used to display the visit count from this website. Previously, it was functioning fla ...

Is there a way to incorporate a trace in Plotly while also arranging each data point along the x-axis in a unique custom order?

I am facing a challenge in creating a line graph that displays (x, y) coordinates where the x axis represents a date and the y axis represents a value. The dates are formatted like DD-MM-YYYY, for example, 15-04-2015. My initial trace is added with the fo ...

How can a color gradient blend be applied to geometry on multiple axes using THREE.JS shader?

After referencing Apply color gradient to material on mesh - three.js I successfully implemented a flower shader that applies a vertical color gradient across the Zinnia. The colors transition from red to yellow vertically, creating a gradient based on he ...

How to Implement Custom Header in AngularJs during Initialization

Let's say I have a delicious Angular application called chococalateApp that relies on 3 other modules: Product, Sales, and LogIn. My app is based on a RESTful API. After a successful login, the server sends an authentication token that needs to be ap ...

What is the most efficient approach to save a value for future utilization in a subsequent function? I've heard that exploiting global variables is highly unfavorable

So I have this code that can be found at http://jsfiddle.net/8j947/10/. It returns either true or false for the variable isLive. My main concern now is how to utilize the variable onLive in a subsequent function. While I've encountered some solutions ...

Tips for adding a bounding box to an image received from the server

I've got a python server that is returning a collection of bounding boxes post OCR processing (using Tesseract or similar). "bbox": [{ "x1": 223, "y1": 426, "x2": 1550, &q ...

Next.js (TypeScript) - Error: Property 'req' is not recognized on the type 'GetServerSideProps'

Currently, I am tackling a challenge involving the utilization of next-auth and attempting to access the session from within getServerSideProps. However, in order to achieve this, it is essential for me to provide the context request and context response ...

Creating a Nested DataTable: A Step-by-Step Guide

My data structure includes a nested dict presented as follows: var dataSet = [{"s_group": [{"range_name": null, "name": "XXXXXXXXXXXX"}], "configfile": "XXXXXXXXXXX", "src_port": ["0-65535"], ...

When I click a button in d3 to refresh the data on my bar graph, the text fails to update accordingly

I've successfully created a series of data lists that modify the bargraph. Unfortunately, due to their differing x and y values, they end up printing new values on top of existing ones. Shown below is an image illustrating the issue where x and y val ...

Tips for resolving the <!--bindings={ "ng-reflect-ng-for-of": "" }--> bug

Just starting out with Angular and I am having some issues with event binding and property binding in my game-control component. I have a button that, when clicked, adds numbers to an array using setInterval method. I am also passing data between compone ...

Issues with HTML formatting after using JQuery .load()

Hey there StackOverflow community! I've encountered a problem that I need help with. Whenever I use the following code: $("#specificDIV").load("new.html") The HTML content loads into the div, but the CSS and JS don't work properly. The formatt ...

Alert: MongoDBError: Timeout occurred while buffering operation `users.insertOne()` for 10 seconds

While running MongoDB Atlas on node express, I encountered an error when testing with Postman. const express = require('express'); const cors = require('cors'); const mongoose = require('mongoose'); require('dotenv' ...

Converting MongoDB Queries to MySQL in Node: A Step-by-Step Guide

I am currently facing a challenge where I need to migrate my app's database from mongoDB to mysql. The application was initially developed using node.js, but I encountered an issue with the "Create table" query while attempting the conversion process. ...

Switch between different templates by toggling their individual URLs in Angular

Currently, I am trying to implement a way to switch between different views on a single page using Angular. Although I have already set it up with routes, I believe there may be a better practice for my application. I am looking for guidance on how to tog ...

Struggling to make divs reach the bottom of the page? Check out my jsFiddle for a solution!

I'm facing difficulty in extending the left and right divs to the bottom of the page, with no additional space above or below. You can view my progress here: http://jsfiddle.net/qggFz/26/ Appreciate any help, Dale ...