从JS到rails控制器的AJAX请求的控制台错误

时间:2022-10-07 14:31:55

I am having a scenario in which i have to sent a AJAX request from JS to change the boolean value in database column.

我有一个场景,我必须从JS发送一个AJAX请求来更改数据库列中的布尔值。

Ajax code

$.ajax({
        type: "POST",
        url: "/action"
    });

Controller code

def action
  current_user.modal.last.toggle!(:boolean-column-name)
end

It changes the boolean value in table column successfully. But after that i am receiving an error in browser CONSOLE as below

它成功更改了表列中的布尔值。但之后我在浏览器CONSOLE中收到错误,如下所示

POST http://URL/action
500 (Internal Server Error)

I am new to AJAX. What i am doing wrong in ajax request? Thanks in advance.

我是AJAX的新手。我在ajax请求中做错了什么?提前致谢。

2 个解决方案

#1


0  

The 500 (internal server error) means something went wrong on the server's side. It could be several things, but I would start by verifying that the URL and parameters are correct. Also, make sure that whatever handles the request(your action method) is expecting the request as a POST.

500(内部服务器错误)意味着服务器端出现了问题。这可能是几件事,但我首先要验证URL和参数是否正确。此外,请确保处理请求的任何内容(您的操作方法)都希望将请求作为POST。

Write a success callback to see what you are getting in response(the last line in your ruby method returns automatically in ruby) and and handle the response in the success block.

编写成功回调以查看响应中的内容(ruby方法中的最后一行在ruby中自动返回)并处理成功块中的响应。

#2


0  

500 (Internal Server Error)

500内部服务器错误)

This is a server error. As opposed to 40x errors, 500 means that your code caused some sort of issue on the server.

这是服务器错误。与40x错误相反,500表示您的代码在服务器上引起某种问题。

You can check the response you received (which should show the error) by clicking onto the network tab of your browser console (below is Chrome Right-Click -> Inspect Element -> Network -> Preview):

您可以通过单击浏览器控制台的网络选项卡(以下是Chrome右键单击 - >检查元素 - >网络 - >预览)来检查收到的响应(应显示错误):

从JS到rails控制器的AJAX请求的控制台错误

Ultimately, what you have to remember is that if you get a 404 error, it means the resource could not be found; 500 errors are a problem with your code.

最后,您必须记住的是,如果您收到404错误,则表示无法找到资源; 500个错误是您的代码的问题。

--

In your case, I would do the following:

在你的情况下,我会做以下事情:

#app/assets/javascripts/application.js
...
$.ajax({
   type: "POST",
   url: "/action",
   success: function(data){
      // do something on success
   },
   error: function(jqXHR,error, errorThrown) {
     // do something on error
   }
});

Without knowing the specific error, you're going to struggle to fix it.

在不知道具体错误的情况下,您将很难解决它。

#1


0  

The 500 (internal server error) means something went wrong on the server's side. It could be several things, but I would start by verifying that the URL and parameters are correct. Also, make sure that whatever handles the request(your action method) is expecting the request as a POST.

500(内部服务器错误)意味着服务器端出现了问题。这可能是几件事,但我首先要验证URL和参数是否正确。此外,请确保处理请求的任何内容(您的操作方法)都希望将请求作为POST。

Write a success callback to see what you are getting in response(the last line in your ruby method returns automatically in ruby) and and handle the response in the success block.

编写成功回调以查看响应中的内容(ruby方法中的最后一行在ruby中自动返回)并处理成功块中的响应。

#2


0  

500 (Internal Server Error)

500内部服务器错误)

This is a server error. As opposed to 40x errors, 500 means that your code caused some sort of issue on the server.

这是服务器错误。与40x错误相反,500表示您的代码在服务器上引起某种问题。

You can check the response you received (which should show the error) by clicking onto the network tab of your browser console (below is Chrome Right-Click -> Inspect Element -> Network -> Preview):

您可以通过单击浏览器控制台的网络选项卡(以下是Chrome右键单击 - >检查元素 - >网络 - >预览)来检查收到的响应(应显示错误):

从JS到rails控制器的AJAX请求的控制台错误

Ultimately, what you have to remember is that if you get a 404 error, it means the resource could not be found; 500 errors are a problem with your code.

最后,您必须记住的是,如果您收到404错误,则表示无法找到资源; 500个错误是您的代码的问题。

--

In your case, I would do the following:

在你的情况下,我会做以下事情:

#app/assets/javascripts/application.js
...
$.ajax({
   type: "POST",
   url: "/action",
   success: function(data){
      // do something on success
   },
   error: function(jqXHR,error, errorThrown) {
     // do something on error
   }
});

Without knowing the specific error, you're going to struggle to fix it.

在不知道具体错误的情况下,您将很难解决它。