Вход

Просмотр полной версии : Не могу отправить данные на сервер


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");