간단한 json 개체를 클라우드 데이터베이스로 보내려고하지만 오류가 계속 발생합니다. 클라우드 데이터베이스에 대한 읽기, 쓰기 및 복제를 허용하는 권한을 설정했습니다. 아약스 게시물을 다시 작성하여 오류를 해결하지 못한 헤더를 추가했지만 오류가 계속 발생하여 원인을 찾을 수없는 것 같습니다.오류 415 Json 파일을 cloudant 공용 데이터베이스에 게시
정확한 오류는 다음과 같습니다 당신은 잘못된 API 엔드 포인트로 요청을 보내는
POST https://birdoftravel.cloudant.com/questions/_all_docs/ 415 (Unsupported Media Type)
send @ jquery-1.12.4.min.js:4
ajax @ jquery-1.12.4.min.js:4
saveQuestionToDB @ saveQuestion.js:54
(anonymous) @ saveQuestion.js:77
dispatch @ jquery-1.12.4.min.js:3
r.handle @ jquery-1.12.4.min.js:3
function QuestionObject(q, o, a){
this.question = q;
this.options = o;
this.answer = a;
}
function createQuestion(){
var q = $('#question').val();
console.log("question: "+q);
var o = $('#options').val().split(',');
var a = $('#answer').val();
var question = new QuestionObject(q,o,a);
var jsonQ = JSON.stringify(question);
console.log("json string"+jsonQ);
$('#question').val("");
$('#options').val("");
$('#answer').val("");
return jsonQ;
}
function saveQuestionToDB(){
var urlDB = 'https://birdoftravel.cloudant.com/questions/_all_docs/';
var data = createQuestion();
console.log("data :"+data);
/* $.ajax({
url: urlDB,
type: "POST",
dataType: "application/json",
data: data,
success: function(){
alert('The question was posted!');
},
error: function(err){
console.log("error: "+err+" question not posted: "+data);
}
});*/
$.ajax({
type: "POST",
url: urlDB,
data: data,
success: function(d){
console.log("question was posted!"+d);
},
error: function(e){
console.log("not posted! "+e);
},
dataType: "application/json",
});
}
$('#btn').click(function(e){
saveQuestionToDB();
e.preventDefault();
});
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="quizStyle.css">
<script
src="https://code.jquery.com/jquery-1.12.4.min.js"
integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
crossorigin="anonymous">
</script>
<title>Add a new quiz</title>
</head>
<body>
<h2>Enter a new quiz</h2>
<form id="newQuizForm">
Insert question:<br>
<input type="text" id="question"><br>
Insert optional answers seperated by ;<br>
<input type="text" id="options"><br>
Insert correct answer:<br>
<input type="text" id="answer"><br>
<input type="submit" id="btn" value="Save question">
</form>
<script src="saveQuestion.js"></script>
</body>
</html>
동일한 415 오류가 발생합니다. 또한 우편 배달부와 함께 게시물을 보내려고했지만 동일한 오류가 발생합니다. { "오류": "bad_content_type" "이유" "콘텐츠 형식이 응용 프로그램/JSON해야한다"}는 GET 보내기 가 작동하지 않는 이유를 정말 이해가 안 돼요,하지만 작동합니다. – Karima
콘텐츠 유형에 대한 추가 매개 변수를 json으로 보내면 json의 utf-8에 관한 오류가 발생합니다. – Karima
원래 응답의 업데이트를 확인하십시오. Cloudant에서 CORS를 사용하려면 ** 계정 ** 탭을 열고 ** CORS **를 선택하십시오. – ptitzler