skip to Main Content

I have a WordPress plugin. There is a form in the plugin’s index.php file. I want to send this form data to my article.py file in pythonanywhere, save it here and then send a response to index.php. But I think I’m doing something wrong somewhere. I’ve been trying for about 12 hours but I can’t do it. I’m not very knowledgeable about these issues. I will write the codes I use below. I keep getting CORS Policy error and I have read all forum pages and documents about it.

Here’s the error I’m getting:

Access to fetch at ‘https://metehanz.pythonanywhere.com/submit’ from
origin ‘http://deneme.local’ has been blocked by CORS policy: No
‘Access-Control-Allow-Origin’ header is present on the requested
resource. If an opaque response serves your needs, set the request’s
mode to ‘no-cors’ to fetch the resource with CORS disabled.

When I run the article.py file I installed, I get the error "flask_cors no module named". So I deleted the .virtualenvs/ file and reinstalled it, activated it and entered the file path of the virtual environment in the Virtualenv section of the web panel (/home/metehanz/.virtualenvs/myenv). But the error persists. So I thought I might have installed article.py in the wrong place, so I deleted it and installed it next to flask_app.py, but the error persists.

First, the data sending codes in index.php:

fetch("http://metehanz.pythonanywhere.com/submit", {
                        method: "POST",
                        body: formData
                    })
                        .then(response => {
                            if (!response.ok) {
                                throw new Error('Network response was not ok');
                            }
                            return response.text();
                        })
                        .then(data => {
                            console.log(data);
                            // Başarılı cevap alındığında yapılacak işlemler buraya eklenebilir
                        })
                        .catch(error => {
                            console.error('There has been a problem with your fetch operation:', error);
                        });

Now I’m going to write the code in the article.py file. Don’t worry, I installed things like flask_cors with the pip install command. But when I run the file on the site, I get the error "flask_cors no module named".

from flask import Flask, request
from flask_cors import CORS

app = Flask(__name__)
CORS(app)  # Tüm kaynaklara erişime izin verir

@app.route('/submit', methods=['POST'])
def submit_data():
    1_key = request.form.get('one')
    2_key = request.form.get('two')
    3_key = request.form.get('three')

    # Burada gelen verileri işleyin ve gerektiğinde yanıt döndürün

    return 'Veriler başarıyla alındı.'

if __name__ == '__main__':
    app.run(port=8081)  # veya başka bir port numarası seçebilirsiniz

2

Answers


  1. You need to allow access to your Flask app from http://deneme.local. You would do that by changing this line:

    CORS(app)
    

    …to this:

    CORS(app, origins=[“http://deneme.local”, "https://deneme.local"])
    
    Login or Signup to reply.
  2. You can narrow down your problem by testing this one-line python file:

    import flask_cors
    

    I suspect you’ll find that the python you run your flask script with does not see the flask_cors module that you installed. It’s a common problem. Think about the different environments or different Python executables that you have, and provide more information about these if you need help. (It’s likely that "Don’t worry, I installed things like flask_cors with the pip install command." does not tell the whole story, otherwise you would not have a question here.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search