Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Не могу отправить данные на сервер (https://javascript.ru/forum/misc/35726-ne-mogu-otpravit-dannye-na-server.html)

dmitry111 20.02.2013 10:52

Не могу отправить данные на сервер
 
Не могу понять причину.
Отправляю через POST

App.prototype.sending = function (data, url) {
		var param = "data=" + data,
			xhr = new window.XMLHttpRequest();

		xhr.open("POST", url, true);
		xhr.setRequestHeader("Content-Type", "application/json");
		xhr.onreadystatechange = function () {
			if (xhr.readyState === 4 && xhr.status === 200) {
				alert(xhr.responseText);
			}
		};
		xhr.send(param);
	};

dmitry111 20.02.2013 10:54

тоже самое, через GET - работает:

App.prototype.sending = function (data) {
		var param = "?data=" + data,
			xhr = new window.XMLHttpRequest();

		xhr.open("GET", this.url + param, true);
		xhr.setRequestHeader("Content-Type", "application/json");
		xhr.onreadystatechange = function () {
			if (xhr.readyState === 4 && xhr.status === 200) {
				alert(xhr.responseText);
			}
		};
		xhr.send(null);
	};

danik.js 20.02.2013 11:24

dmitry111, ты выставляешь неверный Content-Type. Правильно так:
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");


Часовой пояс GMT +3, время: 16:30.