0

Feel like I'm missing something obvious (I don't work with javascript or jQuery, I'm just playing around with it in my spare time), but I can't figure out why my ajax request won't work - can't find the answer anywhere on google either.

When I try an ajax request with the below code, it fails with the error: ajax request failing with "TypeError: Cannot read properties of undefined (reading 'open')" - it doesn't even hit my api endpoint

$.ajax({
        url: window.location.origin + "/api/test",
        type: "GET",
        dataType: "json",
        cache: false,
        contentType: "application/json"
    })
        .done(function (json) {
            conole.log(json);
        })
        .fail(function (xhr, status, error) {
            console.log(xhr);
            console.log(status);
            console.log(error);
        });

If I do it with vanilla javascript, it works fine:

var r = await fetch(window.location.origin + "/api/test", {
        method: "GET"
    });

I've tried messing around with all kinds of properties in the ajax request, but to no avail. What am I missing?

9
  • I suspect this is the xhr.open() call internal to $.ajax(). But the only way xhr could be undefined is if there's an error calling new window.XMLHttpRequest() to create the XHR object. Commented May 2, 2024 at 22:26
  • Is it possible you've redefined XMLHttpRequest somewhere in your code? Commented May 2, 2024 at 22:27
  • Not in my code - this is the only javascript code I have written - it it part of an asp net core app though - which has other libraries used by the page. Do you think they could be interfering? Commented May 2, 2024 at 23:00
  • I would be surprised if jQuery were incompatible with ASP.Net. They're both too popular to not work together. Commented May 2, 2024 at 23:02
  • 1
    @CarenRose - good spot, but was just a copy/paste error, the done function never gets hit. Commented May 3, 2024 at 16:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.