/    Sign up×
Community /Pin to ProfileBookmark

jQuery ajax – best practice

I’ve been using jQuery ajax to load contents onto my website via events for a while.

Is this still the best approach or should I be using something like fetch to do this?

fetch appears to only use json and my content pages are html pages.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 25.2020 — @kiwis80#1619892 fetch appears to only use json and my content pages are html pages.[/quote]No, JSON is widely used with fetch, but not necessaryly. You can load text as well:
fetch('your-url').then(resp => {
// return resp.json();
return resp.text(); // Return text instead of decoding JSON
}).then(responseText => {
document.querySelector('selector-of-destination').innerHTML = responseText;
});
If your jQuery ajax is working and you are using jQuery anyway I do not see any reason for changing the working procedure.
Copy linkTweet thisAlerts:
@kiwisauthorJun 25.2020 — @Sempervivum#1619894

Thanks awesome, thanks.

Looking to use fetch over jQuery for a new project. Trying to move away from jQuery overtime.

Out of interest, how can I pass in a few variables to the fetch request, such as an ID number
Copy linkTweet thisAlerts:
@SempervivumJun 25.2020 — how can I pass in a few variables to the fetch request, such as an ID number[/quote]That's a bit different when using the fetch API and there are several ways to do it:
  • 1. Create a query string by yourself in order to add GET parameters:
    fetch(<span><code>yourscript.php?id=${theid}&amp;amp;var2=${anotherVar}</code></span>).then(resp =&gt; {
    return resp.text();
    }).then(responseText =&gt; {

  • 2. Use FormData:
    <i>
    </i> formData = new FormData();
    formData.append('id, theid);
    formData.append('var2', anotherVar); <br/>
    fetch('yourscript.php', {
    method: 'post',
    body: formData
    }).then(resp =&gt; {

  • 3. Use JSON (appropriate when the structure of the parameters is more complex). Described here:

    https://stackoverflow.com/questions/29775797/fetch-post-json-data

    but I didn't use it myself yet.


  • Looking to use fetch over jQuery for a new project. Trying to move away from jQuery overtime.[/quote]Agree, I've done the same in the past.
    Copy linkTweet thisAlerts:
    @VITSUSAJun 26.2020 — @kiwis80#1619892 I agree with Sempervivum, JSON is widely used with fetch, but not necessarily. You can load text as well.
    Copy linkTweet thisAlerts:
    @bwclovisJun 27.2020 — I'd also recommend looking into Axios.
    ×

    Success!

    Help @kiwis spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...