我在从Web服务加载时遇到问题,但是收到错误:
无法加载资源:服务器响应状态为404()
请提供任何帮助。
您还可以看到相同代码的小提琴。
JSFiddle链接: https://jsfiddle.net/Jakwakwa/LagLxpq7/6/
// Retrieve comments via Web Service
// -----------------------------------
$(function () {
var $comments = $('#comments-ajax');
// GET INFO FROM DATA FILE
$.ajax({
type: 'GET',
url: 'https://e325wwg595.execute-api.eu-west-1.amazonaws.com/prod/comment?blogId=[blogId]',
// url: 'https://e325wwg595.execute-api.eu-west-1.amazonaws.com/prod/comment',
// url: '/data/comments.json',
success: function(comments) {
$.each(comments, function(i, comment) {
$comments.append('<div class="comments-block"><span class="comment-author">James Dean </span><span class="comment-date">27 April 2017 </span><span class="comment-wrote">- wrote</span><p>' + comment.comment_text + '</p></div>');
});
},
error: function() {
alert('error loading data from web service');
}
}); // end .ajax get DATA
// ----------------
$('#post-comment').on('click', function() {
var comment_input = document.getElementById("comment-input").value;
var JSONObject = { "blogId" : 234, "comment_text": comment_input };
$.ajax({
type: 'POST',
url: 'https://e325wwg595.execute-api.eu-west-1.amazonaws.com/prod/comment/',
// url: '/data/comments.json',
data: JSON.stringify(JSONObject),
// data: JSONObject,
dataType: 'json',
success: function(newComment) {
$comments.append('<div class="comments-block"><span class="comment-author">James Dean </span><span class="comment-date">27 April 2017 </span><span class="comment-wrote">- wrote</span><p>' + newComment.comment_text + '</p></div>');
},
error: function(newComment) {
alert('error saving order');
}
});
});
});
&#13;
.comments-expanded-container {
background-color: #efefef;
padding-top: 25px;
padding-bottom: 25px;
padding-left: 20px;
padding-right: 20px;
width: 100%;
}
.comment-input-field {
width: 90%;
height: 180px;
border: none;
border-bottom: 2px solid #bababa;
}
.comments-btn {
background-color: #86bdbb;
border: none;
color: #fff;
font-size: 24px;
padding-left: 50px;
padding-right: 50px;
padding-top: 10px;
padding-bottom: 10px;
float: right;
margin-right: 50px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-lg-11 offset-md-1">
<hr>
<div class="comments-expanded-container toggle-container clearfix">
<div id="comments-ajax">
</div>
<div>
<input type="text" id="comment-input" class="comment-input-field" name="comments">
<button id="post-comment" class="comments-btn">Comment</button>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
您必须为&#34; blogId&#34;传递有效的ID参数。
另一个细节是此请求可能需要身份验证令牌