How do I execute JS XHR responses in Dojo?

What I'm looking for is similar to 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 (Accept-header bit) jQuery executes it (last script parameter).

I am trying to get the same effect in Dojo. My best guess:

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

      

handleAs: "javascript"

should call Dojo to execute the response as JS. My problem is that I can't figure out how to set up the header so that my web server (a block respond_to do |format|

in Rails) knows what to return.

+1


source to share


2 answers


I believe the answer is this:



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

      

+3


source


Not sure about dojo myself, but I know that the fucking thing (dojo dev host) is available on # dojo channel via freenode irc, that is, if no one else can give you an answer.



0


source







All Articles