mapbox / mapbox/mapbox-gl-directions

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Open
#177 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

auto-triaged question
Dominant language
JavaScript
Stars
255
Forks
130
PR merge metrics
No merged PRs in 30d

Description

I'm trying to set up a simple map that automatically loads directions on the load of the map. Here is my HTML & JS:

<!DOCTYPE html>
<html>

	<head>
		<meta charset='utf-8' />
		<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.2/mapbox-gl.js'></script>
		<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.2/mapbox-gl.css' rel='stylesheet' />
	</head>
	
	<body>
		<script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v3.1.2/mapbox-gl-directions.js'></script>
		<link rel='stylesheet' href='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-directions/v3.1.2/mapbox-gl-directions.css' type='text/css' />
		<div id='map' style='width: 800px; height: 600px;'></div>
		
		<script>
			mapboxgl.accessToken = '########';
			
			var directions = new MapboxDirections({
				unit: 'metric',
				profile: 'mapbox/walking',
				container: 'directions',
				controls: { inputs: false }
			});
			
			var map = new mapboxgl.Map({
				container: 'map',
				style: 'mapbox://styles/andyfurniss/cjgkq5xda006z2rs21ash68cs',
				center: [116.0724, 5.9749],
				zoom: 10
			});
			
			map.addControl(directions);
			
			map.on('load', function () {
				directions.setOrigin([110.33333, 1.55]);
				directions.setDestination([116.0724, 5.9749]);
			});
		</script>
	</body>

</html>

This is just giving me the following error:

Failed to load https://api.mapbox.com/directions/v5/mapbox/mapbox/walking/110.33333%2C1.55%3B116.0724%2C5.9749.json?geometries=polyline&alternatives=true&steps=true&overview=full&access_token=undefined: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.

What am I doing wrong? Apologies if I'm being a bit stupid, this is the first time I've using Mapbox!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the MapboxDirections initialization and the generated Directions API request, especially how the access token is supplied. Done means the request is accepted and directions load when the map finishes loading.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api, frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.