# Routes from raw js (using XMLHttpRequest)

**URL:** <https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643>\
**Category:** Rails\
**Created:** [July 24, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643 "2009-07-24T14:10:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 24, 2009, 2:10pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/1 "2009-07-24T14:10:46Z")

</div>

I am making an ajax call from js to call a method (assocboxchange) in  
my controller (AssociatesController), using XMLHttpRequest. I know  
the XMLHttpRequest works fine because I use it in other places with  
success. My problem is my URL I am using for this request doesn;t  
access the method in my controller which I (think) I am specifying. I  
am having it post to /channels/assocboxchange/" with certain  
parameters, but the method in my controller never is gotten to. I know  
the problem is not in my js or in making the post request – it is  
posting somewhere and I believe that because in firefox, the error  
concole is clean. Can someone tell me from my js below where I am  
posting, or why my controller method isnt getting the post? Thanks,  
Janna B

//this function is getting called by an onchange in the associates  
select  
function assocchange(therow){  
var s = document.getElementById(“assoc”+therow).value;  
var url = “/channels/assocboxchange/”  
var parameters = “assoc=” + escape(encodeURI(s)) + “&id=” + therow;  
formid=“assoc” + therow;  
makePOSTRequest(url, parameters, formid)  
}

function makePOSTRequest(url, parameters, formid){  
req3 = new XMLHttpRequest();  
if (req3) {  
req3.onreadystatechange = alertContents(formid);  
req3.open(‘POST’, url, true);  
req3.setRequestHeader(“Content-type”, “application/x-www-form-  
urlencoded”);  
req3.setRequestHeader(“Content-length”, parameters.length);  
req3.setRequestHeader(“Connection”, “close”);  
req3.send(parameters);  
}  
}

function alertContents(formid) {  
if (req3.readyState == 4) {  
if (req3.status == 200) {  
result = req3.responseText;  
document.getElementById("’"+formid+"’").innerHTML =  
result;  
} else {  
alert(‘There was a problem with the request.’);  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 24, 2009, 4:20pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/2 "2009-07-24T16:20:08Z")

</div>

On Fri, 2009-07-24 at 05:09 -0700, JannaB wrote:

> it is posting somewhere and I believe that because in firefox,  
> the error concole is clean.

The console tab in the Firebug plugin is what you want to be looking at.  
It will tell you if / where your app is making requests and the  
parameters it’s passing.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 25, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/3 "2009-07-25T20:14:08Z")

</div>

What shows up in the log when you try this action? That will give you  
more info…

–Matt J.

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 25, 2009, 10:08pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/4 "2009-07-25T22:08:52Z")

</div>

What a great tool. When I click the select box, in firebug, I see a  
RED:

POST [http://localhost:3000/channels/assocboxchange/](http://localhost:3000/channels/assocboxchange/)

with a littel red circle with a white X in it. IF I look at the  
parameters, those seem to be passed properly.

In my ChannelsController, I have:

def assocboxchange  
puts “you;re in”  
render :action =\> ‘display’  
end

Which doesn;t get hit – obviously, that red POST line is trying to  
tell me something is wrong…but what? -Janna B

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 27, 2009, 12:41pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/5 "2009-07-27T12:41:55Z")

</div>

Oh, wait, I need to pass the parameter of the authenticity\_token to  
the url. How can I do this?

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 28, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/6 "2009-07-28T17:18:05Z")

</div>

You’ll need to grab the token on the server side - see line 1065 of  
prototype\_helper.rb for more detail.

But before you do that, you may want to take a look at the rest of  
PrototypeHelper; what you’re doing looks to be well-covered by the  
methods already available, which could save you a lot of low-level  
AJAX hackery.

–Matt J.

---

<div class="post-metadata">

**Author:** ![mistressjanna](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3ab097/32.png) [@mistressjanna](https://www.ruby-forum.com/u/mistressjanna)\
**Post date:** [July 27, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/routes-from-raw-js-using-xmlhttprequest/172643/7 "2009-07-27T12:43:05Z")

</div>

Oh, wait, I need to pass the parameter of the authenticity\_token to  
the url. How can I do this? -Janna
