简体   繁体   中英

How to send an http request with parameters to rails from javascript

I'm attempting to test a sample project at

http://162.243.232.223:3000/api/query

This is a rails project.

The rails controller is looking for a params hash like params[:query]['input']

In javascript, I am using jquery like this:

$.get('http://162.243.232.223:3000/api/query') 

which get the expected default output.

However, I'd like to send in my own params more along the lines of this:

$.get('http://162.243.232.223:3000/api/query', { 'input': 'my_input' })

However, for whatever reason, this is returning errors, the

{ 'input': 'my_input' }

argument is not being passed as

params[:query]['input'] 

into Rails.

Any ideas on how I could start getting non-default responses back from Rails?

Thanks

The way you have it now you would access it simply with params[:input] . The params hash gets all path, get and post variables in a single hash.

In some cases when the server is not getting the input params like this how you had mentioned you need to apply this instead:

$.param({
        json: JSON.stringify({
            'input': 'my_input'
        })
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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