skip to Main Content

I need to write function which prepare me a HTML to inuput into my site, I tried like this

function prepareHTML(valId){
  $.ajax({
    type: "POST",
    url: "SOAPulr_getData",
    data: {operationId: valId}
  })
  .done(function(result, status, jqXHR){
    var def = $.Deferred();
    var response = jqXHR.responseText;
    var outputHTML;
    [... here i generate HTML code ...]
    def.resolve(outputHTML);
    return def.promise();
  });
}

now when i tried to run this function:

prepareHTML(22)
.done(function(data){
  console.log(data)
});

I get an error:
TypeError: prepareHTML(…) is undefined
when I run prepareHTML(22) there is no error in console.

What I do wrong?

Best regards

ssnake

2

Answers


  1. Chosen as BEST ANSWER

    Yes I can do this like that:

    function prepareHTML(valId){
      res = $.ajax({
        type: "POST",
        url: "SOAPulr_getData",
        data: {operationId: valId}
      });
    
      return res.promise();
    }
    

    and it works but then I must to generate my HTML into done function:

    prepareHTML(22).done(function(result, status, jqXHR){
        var response = jqXHR.responseText;
        [... here i generate HTML code ...]
    });
    

    Question is: is it possible to pre-process the respone to have HTML ready in the done function

    I need something like this:

    prepareHTML(22).done(function(myHTML){
        // here I need ready HTML code prepared into prepareHTML() function
        $("#myId").html(myHTML);
    });
    

  2. prepareHTML has no return statement so it returns undefined

    return $.ajax...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search