I've done my googling and searching for a solution to my problem and haven't found a solid solution in part because of how odd my project is.
I'm trying to make a simple Chat-App with Rails 4 backend using only "vanilla" javascript (no jQuery or Rails helpers allowed). I've got a GET Ajax request working which gathers all of the seeded message data and shows it in the chat. However, when I try to write a POST request to my Rails backend I'm running into two main problems. They may or may not be related to each other.
The first problem, which I've temporarily overcome, is a 422 status error (Unprocessable Entity). I believe this is because my xmlhttp.setRequestHeader
values are wrong?
However, I don't know what else they should be. I've temporarily by-passed this problem by putting a skip_before_filter :verify_authenticity_token
in my Rails MessagesController.
Is there a more elegant way of solving this?
This leads me to my next problem which is a 400 status error (Bad Request). When I look at my error on the Terminal while running my server the problems looks like...
Started POST "/messages" for 127.0.0.1 at 2014-11-26 17:32:48 -0800
Processing by MessagesController#create as */ *
Parameters: {"user"=>"namechatsample", "content"=>"messsagechatsample"}
Completed 400 Bad Request in 4msActionController::ParameterMissing (param is missing or the value is empty: message): app/controllers/messages_controller.rb:17:in
message_params' app/controllers/messages_controller.rb:9:in
create'
Any guidance and help would be appreciated!
Here is my Code.
Ajax Calls
var getRequest = function(callback) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
data = JSON.parse(xmlhttp.responseText);
callback(data.messages)
} else if (xmlhttp.status == 400) {
alert('There was an error 400 for GET')
} else {
alert('something else other than 200 was returned for GET! This status is ' + xmlhttp.status)
}
}
}
xmlhttp.open("GET", "/messages", true);
xmlhttp.send();
}
var postRequest = function(user, message) {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
data = JSON.parse(xmlhttp.responseText);
alert('Post ajax call was a success')
} else if (xmlhttp.status == 400) {
alert('There was an error 400 for POST')
} else {
alert('something else other than 200 was returned for POST! The status is ' + xmlhttp.status)
}
}
}
xmlhttp.open("POST", "/messages", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("user=" + user + "&content=" + message + "");
}
postRequest("namechatsample", "messsagechatsample")
Rails 4 Message Controller
class MessagesController < ApplicationController
skip_before_filter :verify_authenticity_token
def index #returns last 10 messages
render :json => Message.last(10)
end
def create #makes a new message
msg = Message.new(message_params)
msg.save
render :json => msg
end
private
def message_params
params.require(:message).permit(:user, :content)
end
end
If you think the problems in some other file, or code location, please let me know!