Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

sending a post request to an external API server onClick; the easy way

I would like to send a post request to an external server, when a form is submitted.

I am trying to use jquery to execute a function which calls an external server, when a photo is uploaded. What is the simplest way to get this to work?

I found an example on S.OF., but it looks like coffeescript; I'd like to avoid compiling into JS. any ideas for a workaround?

Below, you will find the request that I am trying to execute:

$('send_request').click -> $.ajax({ type: "POST", url: "https://api.projectoxford.ai/vision/v1.0/analyze?" + $.param(params),

    var params = {
        // Request parameters
        "visualFeatures": "Categories,Tags",
        "details": "Celebrities",

        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Content-Type","application/octet-stream");
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","this is a random number");
        }
        // Request body
        data: file,
        processData: false,
        contentType: "application/octet-stream"
        success:
            alert data.id
            return false
        error: (data) ->
            return false
    })
    console.log(send_request);