I would like to pass a variable from jquery and use it with the phoenix path helpers to call an api endpoint which I am want to consume using jquery $.get functions. I can’t find any examples on how to pass that variable to Phoenix. Is this possible and if so, how can I go about it?
My sample code looks something like this:
function get_app_details(application_id){
$.get("<%= api_app_view_path(@conn, :api_view, application_id) %>")
.done(function( data ) {
console.log( "Data Loaded: " + data );
})
.fail(function() {
console.log( "error" );
})
}
Things in <% %> brackets are entirely gone at ‘compile’-time (technically replaced with some internal functionality). The javascript on the other hand gets run on the ‘client’, not the server. If you want a path you either need to encode a list of paths with an appropriate key, or do a lookup back to the server (websockets, REST, whatever) to do it.
In this case your jquery get call could just pass it as a query argument like: