Y
Y
Yevgeni2018-07-17 23:31:46
Flask
Yevgeni, 2018-07-17 23:31:46

Can't make ajax request. How to solve the problem?

Error from chrome console: Error: "Network Error"
exports https://unpkg.com/axios/dist/axios.min.js:8:4479on... index.html:37:29
Request from foreign source blocked: Policy single origin prevents reading the remote resource at 127.0.0.1:5000/user. (Reason: CORS request failed).
flask view:

def create_user():
    if request.method == 'POST':
        # Получаем со стороны клиента json объект
        data = request.get_json()
        # Шифруем пароль
        hashed_password = generate_password_hash(
            data['password'], method='sha256')

        # создаем объект юзера на основе модели(класса) User
        new_user = User(public_id=str(uuid.uuid4),
                        username=data['username'], password=hashed_password, is_admin=False)

        # добавляем пользователя в базу данных
        db.session.add(new_user)
        # подтверждаем сохранение в базе данных
        db.session.commit()

        return jsonify({'callback_message': 'new user created!'})

frontend script:
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    
    <script>
            new Vue({
                el: "#app",
                data:{
                    username: '',
                    password: ''
                },
                methods:{
                    registerUser: function(){
                        axios.post('http://127.0.0.1:5000/user',{
                            username: this.username,
                            password: this.password
                        }).then(function(response){
                            console.log(response)
                        }).catch(function(error){
                            console.log(error)
                        })
                    }
                }
            })
        </script>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Proskurin, 2018-07-17
@Vlad_IT

Google CORS

A
Alexey Cheremisin, 2018-07-18
@leahch

Put the axios.min.js and vue.js files locally on the server and distribute them as static. The browser does not let you in, saying that you violate the use of hits policy. Well, read about CORS - https://ru.wikipedia.org/wiki/Cross-origin_resourc... and https://habr.com/company/pentestit/blog/337146/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question