James A. Rosen
James A. Rosen

Reputation: 65232

How do I execute JS XHR responses in Dojo?

What I'm looking for looks like this in jQuery:

jQuery.ajaxSetup({
  'beforeSend': function(xhr) {
    xhr.setRequestHeader("Accept", "text/javascript");
  }
});
...
$("#my_form").submit({
  $.post($(this).attr("action", $(this).serialize(), null, "script");
  return false;
});

Then, when my server returns some Javascript (the Accept-header bit), jQuery executes it (that last "script" parameter).

I'm trying to get the same effect in Dojo. My best guess is:

form = dojo.byId("my_form")
form.onsubmit = function() {
  dojo.xhrGet({
    url: form.action,
    form: form,
    handleAs: "javascript"
  })
}

The handleAs: "javascript" should cause Dojo to execute the response as JS. My problem is that I can't figure out how to set the header so that my web server (a respond_to do |format| block in Rails) knows what to return.

Upvotes: 1

Views: 2201

Answers (2)

James A. Rosen
James A. Rosen

Reputation: 65232

I believe the answer is:

form = dojo.byId("my_form")
  form.onsubmit = function() {
  dojo.xhrGet({
    url: form.action,
    form: form,
    handleAs: "javascript",
    headers: { "Accept": "text/javascript" }
  })
}

Upvotes: 3

redsquare
redsquare

Reputation: 78667

Not sure about dojo myself but I do know that phiggins (lead dojo dev) is available on the #dojo channel over on freenode irc, that is if nobody else can give you an answer.

Upvotes: 0

Related Questions