简体   繁体   English

Ajax呼叫不再起作用(未授权401)

[英]Ajax call no longer working (401 unauthorized)

The code below was working perfectly on my branch (and even some commits before) and mysteriously I'm now getting the js message: 401 (Unauthorized) after I inspect the browser. 下面的代码在我的分支上运行良好(甚至之前有一些提交),并且在检查浏览器后,我现在神秘地收到了js消息: 401(未经授权)

I have a autocomplete form and when the user selects one field it needs to send this data to be analyzed by the action "get_location" from the same controller. 我有一个自动完成表单,当用户选择一个字段时,它需要发送此数据,并由同一控制器的操作“ get_location”对其进行分析。

home_form.js home_form.js

$(document).ready(function() {
  return $("#search_city").on("change", function() {
    return $.ajax({
      url: "home/get_location",
      type: "GET",
      dataType: "script",
      data: {
        city: $('#search_city').val()
      }
    });
  });

routes.rb routes.rb

  resources :home, only: [:index] do
    get 'get_location', on: :collection
  end

home_controller.rb home_controller.rb

require 'recognize_data'

class HomeController < ApplicationController
  layout "home"
  skip_before_action :authenticate_user!, only: [ :index ]
  include DataX

  def index
  end

  def get_location
    # do some stuff here
  end

end

home.html.erb home.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title></title>
    <%= csrf_meta_tags %>
    <%= action_cable_meta_tag %>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <%= stylesheet_link_tag 'home/manifest.css', media: 'all' %>
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Merriweather:400,300,300italic,400italic,700,700italic,900,900italic' rel='stylesheet' type='text/css'>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB5LELGIW21xr6W1H1NBQNvjqLM3hENa64&libraries=places&language=pt-BR"></script>
    <%= javascript_include_tag 'home/manifest.js' %>
  </head>

<body>

<%= yield %>

<%= yield(:form_js) %>

</body>
</html>

I had two issues with my code: 我的代码有两个问题:

1- Yes, I forgot to put the last }); 1-是的,我忘记了最后一个}); to close the js function 关闭js功能

$(document).ready(function() {
  return $("#search_city").on("change", function() {
    return $.ajax({
      url: "home/get_location",
      type: "GET",
      dataType: "script",
      data: {
        city: $('#search_city').val()
      }
    });
  });
});

2 - I was using skip_before_action :authenticate_user!, only: [:index] on home_controller and it was causing conflicts with the action get_location. 2-我在skip_before_action :authenticate_user!, only: [:index]使用skip_before_action :authenticate_user!, only: [:index] ,它与动作get_location引起冲突。 So I removed the restriction from the index, and just left the sentence: skip_before_action :authenticate_user! 因此,我从索引中删除了限制,只留下了以下句子: skip_before_action :authenticate_user! to cover all the actions. 涵盖所有动作。

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

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