Page 1 of 1

Javascript - ajaxJSON vs. fetch

Posted: Mon Dec 06, 2021 1:10 pm
by James-S
This may seems like an elementary question but still coming up on my javascript skills and I was just told fetch is preferred over any ajax calls.

With fetch seeming to be the preferred method to making API calls, is there a difference between it and ajaxJSON? I realize Profound has had ajaxJSON for a while now but should fetch be used instead for new coding for API calls from javascript?

Re: Javascript - ajaxJSON vs. fetch

Posted: Mon Dec 06, 2021 1:52 pm
by Scott Klement
It really doesn't matter... use whatever works best for you.

Fetch is a relative newcomer. We still (sadly) support browsers like Internet Explorer, so we can't use it here at Profound Logic. But, if fetch is availabe in all of the browsers you support in your shop, please feel free to use it.

Personally, I prefer coding my own with fetch or even with XMLHTTPRequest -- for some reason I like that better than ajaxJSON or any of the other ones that Profound supplies.

Re: Javascript - ajaxJSON vs. fetch

Posted: Mon Dec 06, 2021 2:14 pm
by James-S
Thanks Scott!

Re: Javascript - ajaxJSON vs. fetch

Posted: Tue Dec 07, 2021 12:30 pm
by James-S
Scott,

I went with the ajaxJSON as my first foray into this and everything seems to be okay but my response variable is null in the handler function when viewing in the Web Developer Tools debugger. I verified the RPG in the puimapp config is running via debug so I know it is being invoked and not erroring. I check the server logs in the IFS and nothing there either.

I feel like I am missing something and/or misinterpreted the online info. Do you see anything below?

Code: Select all

    // test ajax call
    var ajaxRequest = new ajaxJSON({
      url: "/profoundui/universal/myudfAPI",
      method: "post",
      headers: {"content-type": "application/json"},
      async: true,
      suppressAlert: false,
      params: {
        AUTH: encodeURIComponent(pui.appJob.auth),
        type: "X1",
        key: "TEST1"
      },
      handler : infoHandler
    });

    function infoHandler(response) {
      alert("Continue!");
    }
    
  }

Re: Javascript - ajaxJSON vs. fetch

Posted: Tue Dec 07, 2021 4:51 pm
by Scott Klement
I can't tell much from what you've posted. I would use your Web Dev tools to look at the network request/response, and see if you can see something amiss.