# jQuery.post gets some code but the code isn't evaluated

**URL:** <https://www.ruby-forum.com/t/jquery-post-gets-some-code-but-the-code-isnt-evaluated/144518>\
**Category:** Rails\
**Created:** [July 30, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/jquery-post-gets-some-code-but-the-code-isnt-evaluated/144518 "2008-07-30T16:16:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 30, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/jquery-post-gets-some-code-but-the-code-isnt-evaluated/144518/1 "2008-07-30T16:16:37Z")

</div>

I’m calling one of my controller actions with this remote call:

jQuery.post("/plans/\<%= @plan.id %\>/add\_learning\_objective?name=" +  
objective\_name)

The action does its stuff and then falls through to a .js.rjs file that  
has this code:

page.alert(“in rjs file”)  
page.replace\_html “learning\_objectives”, :partial =\> “test”

Then, nothing happens. In the firebug console, i can see that the  
result of the post call is this:

try {

alert(“in rjs file”);

jQuery("#learning\_objectives").html(“NEW STUFF\n”);

} catch (e) { alert(‘RJS error:\n\n’ + e.toString()); alert('alert(“in  
rjs file”);\njQuery("#learning\_objectives

").html(“NEW STUFF\n”);’); throw e }

But, it looks like this code is being generated but not actually run: if  
i run the contents of the try block in the console then it does what  
it’s supposed to do. Can anyone explain what’s going on here?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 30, 2008, 8:56pm UTC](https://www.ruby-forum.com/t/jquery-post-gets-some-code-but-the-code-isnt-evaluated/144518/2 "2008-07-30T20:56:13Z")

</div>

> jQuery.post("/plans/\<%= @plan.id %\>/add\_learning\_objective?name=" +  
> objective\_name)

You need to specify the type option as “script” to have it executed.  
(See the docs for jQuery.ajax(), where it is the dataType option. The  
“type” option in $.post is passed to $.ajax as the dataType option)

Rein

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 30, 2008, 7:45pm UTC](https://www.ruby-forum.com/t/jquery-post-gets-some-code-but-the-code-isnt-evaluated/144518/3 "2008-07-30T19:45:58Z")

</div>

> jQuery.post("/plans/\<%= @plan.id %\>/add\_learning\_objective?name=" +  
> objective\_name)

You need to specify the type option as “script” to have it executed.  
(See the docs for jQuery.ajax(), where it is the dataType option. The  
“type” option in $.post is passed to $.ajax as the dataType option)

Rein

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [July 31, 2008, 11:33am UTC](https://www.ruby-forum.com/t/jquery-post-gets-some-code-but-the-code-isnt-evaluated/144518/4 "2008-07-31T11:33:09Z")

</div>

Thanks Rein, that was exactly it: using .ajax instead of .post worked  
perfectly:

//doesn’t evaluate the response, ie doesn’t run the javascript in my  
.js.rjs file  
jQuery.post("/plans/\<%= @plan.id %\>/add\_learning\_objective?name=" +  
objective\_name);

//this works  
jQuery.ajax({  
type: “POST”,  
url: ("/plans/\<%= @plan.id %\>/add\_learning\_objective?name=" +  
objective\_name),  
dataType: “script”  
})

Thanks again  
max
