简体   繁体   English

添加参数时,为什么我的ajax获取请求找不到我的处理程序路由?

[英]Why can't my ajax get request find my handler route when I add arguments?

I am building a web app that displays data about flowers that is stored in my local server running bottle. 我正在构建一个Web应用程序,该应用程序显示有关存储在本地服务器运行瓶中的花朵的数据。 My front end is html, js with ajax; 我的前端是html,带ajax的js; My back end is python with bottle 我的后端是带瓶的蟒蛇

In the browser there is an empty div in which the data is to be displayed. 在浏览器中,有一个空的div,其中将显示数据。 Below it there is a row of images. 在它的下面有一排图像。 When the user clicks on an image the data should display in the div above. 当用户单击图像时,数据应显示在上方的div中。

I tried using $.ajax instead of $.get, and I'm getting the same result. 我尝试使用$ .ajax而不是$ .get,并且得到了相同的结果。

This is my event listener in js: 这是我在js中的事件监听器:

$('.image').click((e)=>{
  $('.selected').removeClass('selected');
  $(e.target).addClass('selected'); // just a visual indication

  $.get('/flowerdesc/'+$(e.target).attr('id')).done((data)=>{
    flowerInfo = JSON.parse(data);
    $('#flower-title').empty();
    $('#flower-title').html(flowerInfo.name);
    $('.desc-text').empty();
    $('.desc-text').html(flowerInfo.description);
  })
})

This is my handler for this request: 这是我的请求处理程序:

@get('/flowerdesc/<flower>')
def get_flower_desc(flower):
  return json.dumps(data[data.index(filter(lambda f: f.name == flower, data)[0])])

(data is an array of dictionaries, each containing data of a single flower) (数据是字典的数组,每个字典包含一朵花的数据)

I am getting a 404 error (the function get_flower_desc is not executed at all) that possibly is happening because of the argument, because whenever I use aa function with no parameters and pass in no arguments I am getting the result that I'm expecting. 我收到了可能由于参数而发生的404错误( get_flower_desc不执行get_flower_desc函数),因为每当我使用不带参数且不传入任何参数的aa函数时,我都会得到期望的结果。

I found that I had to formulate an AJAX request quite precisely to get it to work well with Bottle in a similar scenario. 我发现我必须非常精确地制定一个AJAX请求,以使其在类似的情况下与Bottle一起很好地工作。

Here is an example with a GET request. 这是一个带有GET请求的示例。 You could attach this function to the event handler or move it directly to the event handler. 您可以将此函数附加到事件处理程序,也可以将其直接移动到事件处理程序。

function getFlowerData(id) {
  $.ajax({
    type: "GET",
    cache: false,
    url: "/flowerdesc/" + id,
    dataType: "json", // This is the expected return type of the data from Bottle
    success: function(data, status, xhr) {
  $('#flower-title').html(data['name']);
  $('.desc-text').html(data['description']);
    },
    error: function(xhr, status, error) {
      alert(error);
    }
  });
};

However, I found better results using a POST request from AJAX instead. 但是,我发现使用AJAX的POST请求获得了更好的结果。

function getFlowerData(id) {
  $.ajax({
    type: "POST",
    cache: false,
    url: "/flowerdesc",
    data: JSON.stringify({
        "id": id,
        }),
    contentType: "application/json",
    dataType: "json",
    success: function(data, status, xhr){
      $('#flower-title').html(data['name']);
      $('.desc-text').html(data['description']);
    },
    error: function(xhr, status, error) {
      alert(error);
    }
  });
};

For the POST request, the backend in Bottle should look like this. 对于POST请求,Bottle中的后端应如下​​所示。

@post("/flowerdesc") # Note URL component not needed as it is a POST request
def getFlowerData():
    id = request.json["id"]
    # You database code using id variable
    return your_data # JSON

Make sure your data is valid JSON and that the database code you have is working correctly. 确保您的数据是有效的JSON,并且您拥有的数据库代码可以正常工作。

These solutions using AJAX with Bottle worked well for me. 这些将AJAX与Bottle结合使用的解决方案对我来说效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM