@zlodiak

Почему при POST-запросе в адресной строке есть GET-параметры?

Есть форма и ajax-запрос, который в формате POST отправляет данные формы на сервер:
<div class="auth-form">
	<input type="text" name="login" placeholder="login" id="login">
	<input type="password" name="password" placeholder="password" id="password">
	<button id="submitBtn">Send</button>
</div>

<script type="text/javascript">
	const submitBtnElem = document.getElementById('submitBtn');

	if(submitBtnElem) {
		submitBtnElem.addEventListener('click', auth);
	}

	function auth() {
		const login = document.getElementById('login').value;
		const password = document.getElementById('password').value;		
		const xhr = new XMLHttpRequest();
		xhr.open("POST", "auth_request", true);
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.onreadystatechange = function() {
		  if (this.readyState != 4) return;
		  console.log( this.responseText );
		}
		xhr.send(`login=${login}&password=${password}`);
	}
</script>


После того как пользователь заполняет форму и кликает кнопку Send, запрос уходит на сервер. Но не выполняется на сервере:
5de4d3532ac25274794050.png

Почему не выполняется это меня не очень беспокоит, меня беспокоит в первую очередь присутствие в адресной строке GET-параметров:
http://127.0.0.1:5000/?login=&password=

Помогите пожалуйста понять причину их появления. Ведь в ajax-запросе я указал метод POST и заголовок:
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");


При этом контролер моего фреймворка принимает только POST:
@app.route('/auth_request', methods=['POST'])
def auth_request():
	.......
  • Вопрос задан
  • 96 просмотров
Пригласить эксперта
Ответы на вопрос 1
KickeRocK
@KickeRocK
FrontFinish
<form method="post" class="auth-form">
  <input type="text" name="login" placeholder="login" id="login">
  <input type="password" name="password" placeholder="password" id="password">
  <button id="submitBtn">Send</button>
</form>

const submitBtnElem = document.getElementById('submitBtn');

  if(submitBtnElem) {
    submitBtnElem.addEventListener('click', auth(e));
  }

  function auth(e) {
e.preventDefault();
    const login = document.getElementById('login').value;
    const password = document.getElementById('password').value;		
    const xhr = new XMLHttpRequest();
    xhr.open("POST", "auth_request", true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function() {
      if (this.readyState != 4) return;
      console.log( this.responseText );
    }
    xhr.send(`login=${login}&password=${password}`);
  }
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы