Passing a javascript variable (string) to the controller via ajax - 404

AaronDT

I am trying to pass a javascript variable from my view to my controller via ajax according to the example shown here: Passing JavaScript variable to ruby-on-rails controller

my controller "pages_controller.rb" contains the following method:

def test_page
    @message= params[:id]
    puts @message
end

the respective view "test_page.html.erb" contains javascript with an ajax call which is triggered by clicking a button:

<button id="submit_button">Submit Text</button>

<script>
    $("#submit_button").on('click',function(event) {
       var string = "helloWorld";
        $.ajax({
          url: "/test/test_page/?id="+string,
          type: "POST",
          dataType: "text",
          success: function(){
            alert('Saved Successfully');
          },
          error:function(){
           alert('Error');
          }
        });
    });
</script>

In routes.rb I have defined the following route:

post 'test/test_page/:id', to: 'pages#test_page'

However, when I press the button I get the defined error-alert and upon inspecting the browser console I see the following error:

jquery.js:11007 POST https://my-website.com/test/test_page/?id=halloWelt 404 (Not Found)

But I can see that "rake routes" shows that the following route is defined:

POST   /test/test_page/:id(.:format)  pages#test_page

I have tried defining another method in the controller and rerouting, however the 404 error still remains. What can I do to fix this 404 error?

SRack

If you're looking to pass the :id to that route, you can use the following url: https://my-website.com/test/test_page/halloWelt. (i.e. "/test/test_page/" + string)

I believe in your AJAX, for a POST request, if you want to pass any other data you should explicitly pass in the data like below, rather than via url parameters.

$.ajax({
  url: "/test/test_page/" + string,
  type: "POST",
  data: { hi: 'there' },
  ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Problem with passing javascript variable to PHP variable via AJAX

From Dev

Passing String to Controller using AJAX

From Java

Passing String variable from Java to JavaScript via Webview (vice versa)

From Dev

Passing string variable in Javascript

From Dev

Passing a string variable through ajax

From Dev

Javascript Variable passing to PHP with Ajax

From Dev

AJAX passing null string to MVC controller

From Dev

AJAX Passing JSON to Controller as String Returns Null

From Dev

Passing a string to an object in controller using ajax

From Dev

getting error 404 while passing an array through ajax to the controller

From Dev

Rails 4: Passing a variable from controller to javascript

From Dev

Passing variables from javascript via ajax to php

From Dev

passing data from laravel view to controller via ajax onchange event

From Dev

Passing Strongly Typed View Model to Controller as Paramter via Ajax

From Dev

string parameter sent via AJAX to controller not hitting

From Dev

Unable to send string to a controller method via ajax

From Dev

Calling controller method via ajax reach "404 not found"

From Dev

variable is not passing from javascript to php through ajax

From Dev

Ajax data not passing to controller

From Dev

Passing a string array from an ajax function to mvc controller

From Dev

Ajax Post to Controller 404

From Dev

Passing a string from .NET to a javascript variable

From Dev

Passing a string variable into a function parameter in Javascript

From Dev

Passing sql string variable into javascript function

From Dev

passing data from javaScript to MVC controller view ajax

From Dev

passing an array of int to MVC controller using ajax javascript

From Java

Thymeleaf - passing a variable to controller

From Dev

Passing variable to uib controller

From Dev

Button not passing variable to controller

Related Related

  1. 1

    Problem with passing javascript variable to PHP variable via AJAX

  2. 2

    Passing String to Controller using AJAX

  3. 3

    Passing String variable from Java to JavaScript via Webview (vice versa)

  4. 4

    Passing string variable in Javascript

  5. 5

    Passing a string variable through ajax

  6. 6

    Javascript Variable passing to PHP with Ajax

  7. 7

    AJAX passing null string to MVC controller

  8. 8

    AJAX Passing JSON to Controller as String Returns Null

  9. 9

    Passing a string to an object in controller using ajax

  10. 10

    getting error 404 while passing an array through ajax to the controller

  11. 11

    Rails 4: Passing a variable from controller to javascript

  12. 12

    Passing variables from javascript via ajax to php

  13. 13

    passing data from laravel view to controller via ajax onchange event

  14. 14

    Passing Strongly Typed View Model to Controller as Paramter via Ajax

  15. 15

    string parameter sent via AJAX to controller not hitting

  16. 16

    Unable to send string to a controller method via ajax

  17. 17

    Calling controller method via ajax reach "404 not found"

  18. 18

    variable is not passing from javascript to php through ajax

  19. 19

    Ajax data not passing to controller

  20. 20

    Passing a string array from an ajax function to mvc controller

  21. 21

    Ajax Post to Controller 404

  22. 22

    Passing a string from .NET to a javascript variable

  23. 23

    Passing a string variable into a function parameter in Javascript

  24. 24

    Passing sql string variable into javascript function

  25. 25

    passing data from javaScript to MVC controller view ajax

  26. 26

    passing an array of int to MVC controller using ajax javascript

  27. 27

    Thymeleaf - passing a variable to controller

  28. 28

    Passing variable to uib controller

  29. 29

    Button not passing variable to controller

HotTag

Archive