jQuery ajax:error runs even if the response is OK 200

后端 未结 5 421
轮回少年
轮回少年 2021-01-17 15:21

I have a form which submit a form via AJAX with :remote => true. Looking at the server log and FireBug, I get the response 200 OK and it returns JSON in the form of:

5条回答
  •  孤街浪徒
    2021-01-17 16:08

    (On Rails 5.2 and jQuery 2) I spent an hour comparing two bits of UJS code side-by-side to no avail: both return a similar format Javascript function call (to update the front-end), and a status of 200 but one triggers ajax:error and the other doesn't. I was able to eliminate the problem by switching from jquery_ujs to rails-ujs in the application.js manifest:

    //= require jquery2
    // require jquery_ujs  # Removed this by taking away '='
    //= require rails-ujs
    

    Note the hyphen vs the underline!

    Now both calls behave the same (correct) way. I hope this helps someone else.

提交回复
热议问题