Fellow Stranger
Fellow Stranger

Reputation: 34013

Rendering js partial with custom ajax request (instead of remote: true)

I don't want to use jQuery so I'm crafting my own ajax request with the following code:

var xhReq = new XMLHttpRequest();
xhReq.open("GET", href, false);
xhReq.send(null);
var serverResponse = xhReq.responseText;

I'm letting Rails know that it is the JS format by adding format: "js to the params.

The respond_to block seems to work well, but I don't know how to get the code in the js-view to execute. I'm trying a simple alert("hello"); but nothing happens.

Upvotes: 0

Views: 60

Answers (1)

Blue Skies
Blue Skies

Reputation: 2975

Do eval(serverResponse);

Or make a script element with the href instead of doing an XHR request, which may be better for performance.

var scrpt = document.createElement("script");
scrpt.src = href;
document.body.appendChild(scrpt);

Upvotes: 1

Related Questions