XML HTTP Request Status 0 | XM Community
Skip to main content

In Javascript, I tried to use XMLHTTPRequest to access a public github repository file (and convert the string in the file into some js object that I can use later), which works perfectly okay on my terminal using Node; however, it keeps returning a status of 0 (in instead of 200) when I implemented it in Qualtrics. 

My guess is because it’s a cross-domain operation. And I found an instruction page on Qualtrics support that walks you through using SFTP. 

Does anyone have experience with this or some advice as into what to do in this scenario?

Yes, your issue is likely related to cross-origin resource sharing (CORS) restrictions. When you try to access a resource from a different origin (domain) in a web browser, CORS policies can block your request if the server doesn't explicitly allow it.

Cross-Origin Resource Sharing (CORS) - HTTP | MDN (mozilla.org)

For accessing public GitHub files in a web environment like Qualtrics, using the GitHub API with appropriate headers is the recommended approach.

 


Yes, your issue is likely related to cross-origin resource sharing (CORS) restrictions. When you try to access a resource from a different origin (domain) in a web browser, CORS policies can block your request if the server doesn't explicitly allow it.

Cross-Origin Resource Sharing (CORS) - HTTP | MDN (mozilla.org)

For accessing public GitHub files in a web environment like Qualtrics, using the GitHub API with appropriate headers is the recommended approach.

 

I tried to use the github javascript api package octokit, which works on my terminal again. But does anyone know how I would be able to import this package to Qualtrics?


@CindyJ Since you can't use Node.js style imports directly in Qualtrics, you'll need to include the Octokit library using a script tag. You can add external scripts in the Qualtrics survey using the header or footer sections or directly in the question HTML.

<script src="https://cdn.jsdelivr.net/npm/@octokit/rest/dist-web/index.js"></script>


If you encounter CORS issues, ensure your GitHub repository is public. Private repositories require authentication, and you should include a personal access token.


@CindyJ Since you can't use Node.js style imports directly in Qualtrics, you'll need to include the Octokit library using a script tag. You can add external scripts in the Qualtrics survey using the header or footer sections or directly in the question HTML.

<script src="https://cdn.jsdelivr.net/npm/@octokit/rest/dist-web/index.js"></script>


If you encounter CORS issues, ensure your GitHub repository is public. Private repositories require authentication, and you should include a personal access token.

So sorry for the mass questions!! I just want to see if you could briefly check for me if there’s I have any major misunderstanding of what I should be doing:

In my header (in the html editor), I have this:

I’ve also tried this:

, which is what Octokit specified for us to do working with browsers: link is here https://www.npmjs.com/package/octokit

and my javascript editor looks like this: 

now, it gives me the error “Octokit is undefined”. 

(if I use the second screenshot of importing, I would simply use Octokit instead of Octokit.Octokit as the constructor)

Thank you so much!!


@CindyJ 

try checking if octokit is getting loaded correctly by adding delay and initialize rest of code later.

function checkOctokitLoaded() {
if (typeof Octokit !== 'undefined') {
initializeOctokit();
} else {
setTimeout(checkOctokitLoaded, 100); // Check again after 100ms
}
}

 


@CindyJ

try checking if octokit is getting loaded correctly by adding delay and initialize rest of code later.

function checkOctokitLoaded() {
if (typeof Octokit !== 'undefined') {
initializeOctokit();
} else {
setTimeout(checkOctokitLoaded, 100); // Check again after 100ms
}
}

 

Thank you for the advice!

I tried this and logged the number of times this has recursed. And it just seems Octokit was never defined. 

Do you know what other ways people usually use to obtain data from some web server to display on the questions?


@CindyJ 

Try going through this approach javascript - How to use `request.js` by Octokit on browser? - Stack Overflow

How to properly import request.js along with its dependencies in an HTML file for browsers? · Issue #109 · octokit/request.js · GitHub

Hope this helps!


Thank you so much for the help! I will look into it more.


Leave a Reply