No you cannot do that in plain JS. There is only two official libraries available in nodejs and python. Some other languages also support Open Ai with the help of community libraries. But there is no documentation for plain js. Check more on Open Ai library docs
The code works well. But occasionally, it would display an error of "index.html:38 Uncaught TypeError: Cannot read properties of undefined (reading 'forEach') at http.onload (index.html:38:34)". May I ask what would be the solution to this error? Adjusting the prompts usually solve the issue, but it would be much appreciated if I can input any prompts without causing any form of error. Thank You in advance for your help~
For ex. men apparel, vintage suit, front view, ulrarealistic, hyperrealistic, photo realistic would gemerate result. But say if I made it specific like men apparel, t shirt with astronaut design, front view, photorealistic, it would be 50-50. Sometimes it generates, sometimes it gives the error
I ran into a problem and spent an hour looking for a solution. If it helps someone: Error: Failed to download orhanerday/open-ai from dist: The zip extension and unzip/7z commands are both missing, skipping. The php.ini used by your command-line PHP is: C:\xampp\php\php.ini Now trying to download from source In GitDownloader.php line 82: git was not found in your PATH, skipping source download Solution: You will need to manually enable this extension within your php.ini (C:\xampp\php\php.ini) by adding this to extension list: extension=php_zip.dll
Correct me if I'm wring but this can be done with plain js as well without php. Considering we are making simple POST request and nothing else.
No you cannot do that in plain JS. There is only two official libraries available in nodejs and python. Some other languages also support Open Ai with the help of community libraries. But there is no documentation for plain js. Check more on Open Ai library docs
@@hacodes Oh, thanks for clarification. I will do it for sure. Great video nevertheless.
how to catch response api errors if user input illegal words?
You can use try catch in js it will give error in catch
The code works well. But occasionally, it would display an error of "index.html:38 Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')
at http.onload (index.html:38:34)". May I ask what would be the solution to this error? Adjusting the prompts usually solve the issue, but it would be much appreciated if I can input any prompts without causing any form of error. Thank You in advance for your help~
Can you tell which prompt give error?
For ex. men apparel, vintage suit, front view, ulrarealistic, hyperrealistic, photo realistic would gemerate result. But say if I made it specific like men apparel, t shirt with astronaut design, front view, photorealistic, it would be 50-50. Sometimes it generates, sometimes it gives the error
Merci pour ta vidéo !
This goes wrong on me. :( Any update?
What's the issue?
@@hacodes (index):36 Uncaught TypeError: Cannot read properties of undefined (reading 'forEach')
at http.onload ((index):36:13)
const input = document.querySelectorAll('input');
const button = document.querySelector('button')
const imgContainer = document.querySelector('.img-container')
button.onclick = () => {
if(input[0]){
var http = new XMLHttpRequest();
var data = new FormData();
data.append('prompt', input[0].value + input[1].value + input[2].value)
http.open('POST', 'request.php', true)
http.send(data)
http.onload = () => {
imgContainer.innerHTML = ''
var response = JSON.parse(http.response).data
response.forEach(e => {
var img = document.createElement('img')
img.src = 'data:image/jpeg;base64,' + e.b64_json
imgContainer.appendChild(img)
})
}
}
}
You are not getting response from php file, try running PHP file to check what you getting
Api is free are Not
Free for three month
I ran into a problem and spent an hour looking for a solution. If it helps someone:
Error:
Failed to download orhanerday/open-ai from dist: The zip extension and unzip/7z commands are both missing, skipping.
The php.ini used by your command-line PHP is: C:\xampp\php\php.ini Now trying to download from source In GitDownloader.php line 82: git was not found in your PATH, skipping source download
Solution:
You will need to manually enable this extension within your php.ini (C:\xampp\php\php.ini) by adding this to extension list:
extension=php_zip.dll