Access posted json data from ajax in rails controller

后端 未结 2 1885
说谎
说谎 2021-02-09 00:24

I have made an ajax post request for json data and I want to access that data in the rails controller so how can I access the data.

Here is how I posted the data using a

相关标签:
2条回答
  • 2021-02-09 01:07

    You dont need to define type json in your jquery ajax post request. use below code in your javascript

    jQuery.ajax({
              url: 'main/getlocation',
              data: {latitude:latitude,longitude:longitude},
              type: "POST",
              success: function(data) {
                alert("Successful");
              },
              failure: function() {
                alert("Unsuccessful");
              }
            }); 
    

    and now try

    params[:latitude] and params[:longitude] in your controller.
    
    0 讨论(0)
  • 2021-02-09 01:10

    I guess you have assigned longitude and latitude as variables which is accessible by your ajax request. If its not you will not get anything in the controller. Make sure the variables scope is right , then try this :

    jQuery.ajax({
              url: 'main/getlocation',
              data: "latitude=" + latitude + "&longitude=" + longitude,
              type: "POST",
              success: function(data) {
                alert("Successful");
              },
              failure: function() {
                alert("Unsuccessful");
              }
            }); 
    

    Now, I guess you can get the params[:longitude] and params[:latitude] in your controller.

    0 讨论(0)
提交回复
热议问题