Question T872
Visible to All Users

Using JQuery AJAX to post data to SurveyJS instead of XMLHttpRequest

created 7 years ago

I recently asked this question and it was answered professionally by Serge.

However, while I was trying to put the working code into an Adapt Learning Course (https://www.adaptlearning.org/),,) it could not work. I hence asked on the Adapt Learning Community forum and received this reply: https://community.adaptlearning.org/mod/forum/discuss.php?d=3431

Summary of reply from Adapt: I should use JQuery AJAX (because it is inbuilt into the Adapt Learning framework) instead of XMLHttpRequest.

I then tried to make a similar code work in the browser:
Picture1.png

but I received this error:
Capture.JPG

This is a screenshot to show that the XMLHttpRequest approach works to post data to SurveyJS:
Capture1.JPG

Please advise as to how to use JQuery AJAX instead of XMLHttpRequest. Thanks!

Comments (2)

    Hello,

    To be honest - I've not checked it, but I've reviewed our code and found that in AJAX we do not stringify data, e.g. code that updates survey JSON:

    JavaScript
    $.ajax({ url: "UrlToYourWebService", type: "POST", data: { surveyId: yourEditUniqueSurveyI, surveyText : editor.text }, success: function (data) { callback(saveNo, data.isSuccess); }, error: function (xhr, ajaxOptions, thrownError) { callback(saveNo, false); alert(thrownError); } });

    Thanks, Serge
    SurveyJS Team

      Hi Serge,

      You are right!

      I modified the code to be like this and it works in the browser.

      JavaScript
      var data = { postId: 'someID', surveyResult: JSON.stringify(newSurvey.data) }; // You need to know the postId and survey.data $.ajax({ type: "POST", url: "https://dxsurveyapi.azurewebsites.net/api/Survey/post/", data: data, //no further stringification success: success });

      Now for Adapt Learning… Hope it works.