Calling a Rails function via a POST
I am trying to convert a php script to a ruby script that lives inside a RoR app. This is being done from a Javascript block that is outside the RoR app. I have had success calling a function within the RoR app via a GET but I would like to do it using a POST.
That is to say, this works:
function foo() {
var uri = "http://localhost:3000/foo/bar?thing1=654&thing2=what";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("get", uri, true);
xmlhttp.send();
}
This does not:
function foo() {
var uri = "http://localhost:3000/foo/bar";
var params = "thing1=654&thing2=what";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("post", uri, true);
xmlhttp.send(params);
}
Is there something different I need to do to get this working with a POST?
The params that are actually getting passed into the开发者_运维百科 controller are: {"controller"=>"foo", "action"=>"bar"}
Update
Oddly it looks like, based on firebug reports, that it isn't using POST but OPTIONS for some reason. Is there something wrong with the way I've written this?
You might have to send the correct Content-Type header.
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
The issue was, as it turns out, on the live site the url I was pointing was not in the same domain. I had thought it was.
精彩评论