Skip to content

Question: convert jquery generic ajax to fetch syntax #307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jeff1evesque opened this issue Apr 4, 2016 · 3 comments
Closed

Question: convert jquery generic ajax to fetch syntax #307

jeff1evesque opened this issue Apr 4, 2016 · 3 comments

Comments

@jeff1evesque
Copy link

I was wondering if I could get some input. I am trying to convert the following jquery ajax script:

// AJAX Process
  function ajaxCaller(callbackDone, callbackFail, args) {
    // tell jquery to set the contentType
    if (args.contentType === null) {
      args.contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
    }

    // tell jquery to process the data
    if (args.processData === null) {
      args.processData = true;
    }

    $.ajax({
      type: 'POST',
      url: args.endpoint,
      data: args.data,
      dataType: 'json',
      contentType: args.contentType,
      processData: args.processData,
      beforeSend: function() {

        // asynchronous callback
        // callbackBeforeSend();

      }
    }).done(function(data) {

      // asynchronous callback
      callbackDone(data);

    }).fail(function(jqXHR, textStatus, errorThrown) {

      // asynchronous callback
      callbackFail(textStatus, errorThrown);

    });
  }

to the following fetch equivalent :

// AJAX Process
function ajaxCaller(callbackDone, callbackFail, args) {
  // tell jquery to set the contentType
  if (args.contentType === null) {
    args.contentType = 'application/x-www-form-urlencoded; charset=UTF-8';
  }

  // tell jquery to process the data
  if (args.processData === null) {
    args.processData = true;
  }

  // ajax logic
  fetch(args.endpoint, {
    method: 'post',
    body: args.data,
    headers: {
      'Content-Type':  args.contentType
    },
  }).then(function(response) {
    if (response.ok) {
      // asynchronous callback
      callbackDone(response);
    } else {
      // throw custom error
      var error = {
        'statusText': response.statusText,
        'status': response.status
      }
      throw error;
    }
  }).catch(function(e) {
    // asynchronous callback
    callbackFail(e.statusText, e.status);
  });
}

The latter script doesn't work yet, since the catch block properly executes, and displays corresponding error messages within the browser console.log:

fetch-error

This could happening since my backend endpoint, hasn't been adjusted? But, if I know my fetch syntax is correct, I can proceed debugging by creating a trivial backend endpoint, to accommodate the converted fetch script.

@jeff1evesque
Copy link
Author

I've installed fetch via fetch.pp, which can be verified within the corresponding vagrant vm:

vagrant@vagrant-ubuntu-trusty-64:/vagrant/src/js$ npm list -g | grep fetch
└── [email protected]

@jeff1evesque jeff1evesque changed the title Question: convert jquery generic ajax to fetch Question: convert jquery generic ajax to fetch syntax Apr 4, 2016
@mislav
Copy link
Contributor

mislav commented Apr 5, 2016

  1. What is args.data? If it's not a String, you'll have to serialize it yourself.

  2. From the old code, it seems that you want callbackDone() invoked with JSON data from the response. So, you'll want to make the following change then:

         if (response.ok) {
           // asynchronous callback
    -      callbackDone(response);
    +      response.json().then((data) => callbackDone(data));
         } else {

Keep in mind that fetch does less magic things for you when compared to $.ajax. You'll have to do more manually. But it seems that you're really close.

@jeff1evesque
Copy link
Author

I resolved my issue via jeff1evesque/machine-learning#2401. Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants