gpt4 book ai didi

javascript - Ajax 调用 API 返回错误 HTTP/1.1 405 Method Not Allowed

转载 作者:行者123 更新时间:2023-12-02 16:25:39 25 4
gpt4 key购买 nike

我编写了一个非常简单的应用程序(在页面上),其中有一个 map ,用户可以在上面画圆圈。第一个圆圈是源头,接下来的圆圈是目的地。我必须对 API URL 进行 Ajax 调用,才能获取用户在 map 上绘制的圆圈内的所有单元格 ID。

这是我迄今为止尝试过的:

<html>
<head>
<title>Drawing tools</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<link type="text/css" href="res/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript" src="res/jquery.min.js"></script>
<script type="text/javascript" src="res/jquery-ui.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=drawing,places"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">

var latitude;
var longitude;
var radius;

(function () {
var circle;

function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-34.397, 150.644),
zoom: 8
};

var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);

var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.MARKER,
google.maps.drawing.OverlayType.CIRCLE,
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.POLYLINE,
google.maps.drawing.OverlayType.RECTANGLE
]
},
markerOptions: {
icon: 'images/beachflag.png'
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 1,
strokeWeight: 5,
clickable: false,
editable: true,
zIndex: 1
}
});
drawingManager.setMap(map);
google.maps.event.addListener(drawingManager, 'circlecomplete', onCircleComplete);
}

function onCircleComplete(shape) {
var map=shape.getMap();
var circle;
//create an array where we store the circles
if(!map.get('circles')){
map.set('circles',[]);
}
shape.setOptions(
(!map.get('circles').length)
?//first circle
{type:'source',
fillColor:'#ff0000'
}
://other circles
{type:'destination'}
);
//push the circles onto the array
map.get('circles').push(shape);

circle = shape;
radius = circle.getRadius();
center = circle.getCenter();
latitude = circle.getCenter().lat();
longitude = circle.getCenter().lng();

doStuff();
// alert(radius);

}
google.maps.event.addDomListener(window, 'load', initialize);
})();


function doStuff() {
var where_stm = 'within_circle('+latitude+','+longitude+','+radius+')';

$.ajax({
url: 'https://api.dandelion.eu/datagems/v2/SpazioDati/milano-grid/data?$limit=10&$offset=0&$app_id=7b22cb45&$app_key=dc836a05b4f775d8813d253ba07a4570',
type: 'GET',
contentType: "application/json",
dataType: "json",
data: {where:where_stm},
success: function(response) {
var ParsedObject = JSON.parse(response);
console.log(ParsedObject);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("error :"+XMLHttpRequest.responseText);
}
});
}

</script>
</head>
<body>
<div id="container">
<div id="sidebar-left">
<p> Please select your source place by using drawing tools on the map. </p>
<div id="destinations">
Now you can select one or more destination on the map using the same drawing tools
</div>

<button onClick="doStuff()">Run Code</button>

</div>
<div id="map-canvas"></div>
</div>
</body>
</html>

我的问题:当我在 map 上画一个圆圈时,我在控制台上看到以下错误:选项 https://api.dandelion.eu/datagems/v2/SpazioDati/milano-grid/data [HTTP/1.1 405 方法不允许 350ms] 当我点击它时,我看到这个:

enter image description here

我很感激你的所有想法,谢谢

最佳答案

我尝试通过 Postman 发出请求,使用 GET 和(仅!)您在代码片段中提供的 URL:200/OK,没有缺陷。尝试不使用“数据”。它只应该在 POST 请求中使用,而不是在 GET 请求中使用!

但是由于您需要传递“within_circle”数据集,因此您应该考虑使用 POST,它使您可以访问数据属性的使用,或者通过在 URL 中传递数据参数来发出 GET 请求。由于我不了解 API,因此无法为您做出选择。

关于javascript - Ajax 调用 API 返回错误 HTTP/1.1 405 Method Not Allowed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28716476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com