게시물 내용을 ajax를 통해 PHP 파일로 보낼 때 문제가 있습니다. 내 의도는 save.php 파일에 사용될 데이터가 포함 된 객체를 보내는 것이며, 경고 창에 나타나도록 응답을 처리해야합니다.
var iden = true;
var object = {
'titulo': 'In the mountain',
'dest': 'Single of',
'edit': true,
'previ': 'yes',
'iden': iden
};
var xhr = new XMLHttpRequest();
xhr.open('POST', "save.php", false);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send( object);
if (xhr.status == 200) {
xhr.onload = function () {
alert(this.responseText);
};
} else {
alert('error');
}
문제는 데이터를 보내려고 할 때 콘솔에 다음 메시지가 표시된다는 것입니다.
Test.js : 56 [Deprecation] 메인 스레드의 동기 XMLHttpRequest는 최종 사용자의 경험에 해로운 영향을 미치기 때문에 더 이상 사용되지 않습니다. 도움이 더 필요하면 https://xhr.spec.whatwg.org/를 확인 하세요 .
56 행은 다음에 해당합니다.
Xhr.open ('POST', 'save.php', false);
async 매개 변수를 true로 설정하면 조건부 if (xhr.status == 200) 에서 else를 건너 뛰고 오류 경고를 표시합니다.
문제는 어디에 있습니까?
또한 jquery로 완벽하게 작동한다고 말하지만 Javascript로하고 싶습니다.
jquery 사용 :
$.ajax({
url: "save.php",
type: "post",
data: object
}).done(function(data) {
alert(data);
});
편집하다:
console.log (xhr);
XMLHttpRequest {readyState : 1, timeout : 0, withCredentials : false, 업로드 : XMLHttpRequestUpload, onreadystatechange : function…} onabort : null onerror : null onload : null onloadend : null onloadstart : null onprogress : null onreadystatechange : function () ontimeout : null readyState : 4 응답 : ""responseText : ""responseType : ""responseURL : " http : //localhost/save.php"responseXML : null 상태 : 200 statusText : "OK"시간 초과 : 0
이 코드 줄을 변경해보십시오 : xhttp.open("POST", "save.php", false);
세 번째 매개 변수를로 설정하십시오 true
. 작동 할 수 있으면이 예를 따르십시오.
var object = {
'titulo': 'In the mountain',
'dest': 'Single of',
'edit': true,
'previ': 'yes',
'iden': iden
};
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML =
this.responseText;
}
};
xhttp.open("POST", "save.php", true);
xhttp.send(object);
<div id="demo">
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Change Content</button>
</div>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다