Javascript - ajaxJSON vs. fetch

Use this board for starting discussions, asking questions, and giving advice on Web programming for the IBM i platform (and predecessors.)
James-S
Profound User
Posts: 61
Joined: Tue Jun 28, 2016 12:53 pm
First Name: James
Last Name: Sherwood
Company Name: Brunswick Boat Group
City: Knoxville
State / Province: Tennessee
Contact:

Javascript - ajaxJSON vs. fetch

Post 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?
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Javascript - ajaxJSON vs. fetch

Post 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.
James-S
Profound User
Posts: 61
Joined: Tue Jun 28, 2016 12:53 pm
First Name: James
Last Name: Sherwood
Company Name: Brunswick Boat Group
City: Knoxville
State / Province: Tennessee
Contact:

Re: Javascript - ajaxJSON vs. fetch

Post by James-S »

Thanks Scott!
James-S
Profound User
Posts: 61
Joined: Tue Jun 28, 2016 12:53 pm
First Name: James
Last Name: Sherwood
Company Name: Brunswick Boat Group
City: Knoxville
State / Province: Tennessee
Contact:

Re: Javascript - ajaxJSON vs. fetch

Post 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!");
    }
    
  }
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

Re: Javascript - ajaxJSON vs. fetch

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests