First of all I should point out that I'm quite new to Rails (and jQuery) and the interactions between it and JQuery, which may be the reason that some of you may consider this a rather basic hint.
However whilst working on membermeister.com I've recently had some trouble loading JSON data from our Rails backend. It seemed like a very trivial task at first, all we needed was a GET request that was answered by a format.json response from the Rails controller.
Simplified the request looked something like this:
2 url: "/invoices/" + invoiceid + "/pay",
3 type: "GET"
4})
However Rails was returning a '406 Not Acceptable' error, suggesting the request I was sending was not something it was instructed to handle.
In my noobness I tried passing
I also tried
but always got this error:
In the end the fix was quite simple but had evaded me for a while:
followed by
2 xhr.setRequestHeader("Accept", "application/json");
3}
The complete function looked (simplified) like this:
2 url: "/invoices/" + invoiceid + "/pay",
3 type: "GET",
4 beforeSend: setHeader
5}).done(function() {
6...
7});
8
9function setHeader(xhr) {
10 xhr.setRequestHeader("Accept", "application/json");
11}
Once implemented the GET request succeeded (notice the Accept Request Header).
Hopefully this saves someone some grief. Loading JSON from Rails seems well documented, and so ar 406 errors, but I had trouble finding the fix in this case because I wasn't 100% sure the issue was on the jQuery side...
