I am doing a very simple fetch(url) to a URL at localhost/cma_mysql/getstuff.php?username=ppowell777 from localhost:8000/login.html using Python scripts.
The PHP portion that connects to my local MySQL platform works just fine on its own, retrieving data if found and returning a JSON array string, however, localhost:8000/login.html, using fetch(url), can’t reach it itself due to CORS error.
I have tried everything I can think of, and I visited several tutorials on the matter, and everything I tried was blocked by the browser, and since this is my own CMA on my browser on my laptop and nowhere else on earth, I would only need to fix this ultimately for myself at the moment.
Here is the Python code I used to try to connect to the “remote” URL:
import asyncio, logging
#from pyodide.http import pyfetch
from js import JSON
from pyscript import fetch
'''
MySQL is not accessible via PyScript, therefore, you will have to do a URL scrape to a PHP stub on the IIS web server that can access
MySQL for you and perform necessary tasks to return to the PyScript-based CMA
Re: stackoverflow.com/questions/75054667/pyscript-website-showing-no-module-named-mysql
'''
def getLoginCredz(username):
logger = logging.getLogger(__name__)
if username is None or len(username.strip()) == 0:
error = 'username not provided for getLoginCredz()'
logger.error(error)
raise EOFError(error)
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(getUrlBody(LOGIN_SELECT_URL + username))
print(JSON.stringify(json))
except EOFError as error:
logger.error(error)
raise
async def getUrlBody(url):
global json
json = ''
logger = logging.getLogger(__name__)
if url is None or len(url.strip()) == 0:
error = 'url not provided for getUrlBody()'
logger.error(error)
raise EOFError(error)
try:
response = await fetch(url)
json = await response.json
except NameError as error:
logger.error(error)
raise