我正试图在1.11 django wannabe聊天服务器上发布带有javascript的消息。我对javascript很新,所以我发帖和获取对我来说都是新手。如果我在数据库中创建一条消息,它将显示在聊天框中。这让我相信这只是一个发布错误。这是我得到的以下错误:
chat.js:13 POST http://127.0.0.1:8000/chat/api/messages/ 400 (Bad Request)
send @ jquery-3.2.1.min.js:4
ajax @ jquery-3.2.1.min.js:4
r.(anonymous function) @ jquery-3.2.1.min.js:4
send @ chat.js:13
(anonymous) @ (index):186
dispatch @ jquery-3.2.1.min.js:3
q.handle @ jquery-3.2.1.min.js:3
项目的urls.py
url(r'^chat/', include("chat.urls",namespace='chat')),
chat的urls.py
urlpatterns = [
url(r'^$', chat_view, name='chats'),
url(r'^(?P<sender>\d+)/(?P<receiver>\d+)/$', message_view, name='chat'),
url(r'^api/messages/(?P<sender>\d+)/(?P<receiver>\d+)/$', message_list, name='message-detail'),
url(r'^api/messages/$', message_list, name='message-list'),
url(r'^api/users/(?P<pk>\d+)/$', user_list, name='user-detail'),
url(r'^api/users/$', user_list, name='user-list'),
]
java脚本:
//For sending
$(function () {
scrolltoend();
$('#chat-box').on('submit', function (event) {
console.log(event);
event.preventDefault();
var message = $('#id_message');
send('{{ request.user.username }}', '{{ receiver.username }}', message.val());
message.val('');
})
})
更多js
function send(sender, receiver, message) {
$.post('/chat/api/messages/', '{"sender": "'+ sender +'", "receiver": "'+ receiver +'","message": "'+ message +'" }', function (data) {
console.log(data);
var box = text_box.replace('{sender}', "You");
box = box.replace('{message}', message);
$('#board').append(box);
scrolltoend();
})
}
function receive() {
$.get('/chat/api/messages/'+ sender_id + '/' + receiver_id, function (data) {
console.log(data);
if (data.length !== 0)
{
for(var i=0;i<data.length;i++) {
console.log(data[i]);
var box = text_box.replace('{sender}', data[i].sender);
box = box.replace('{message}', data[i].message);
box = box.replace('right', 'left blue lighten-5');
$('#board').append(box);
scrolltoend();
}
}
})
}
function scrolltoend() {
$('#board').stop().animate({
scrollTop: $('#board')[0].scrollHeight
}, 800);
}
关于出了什么问题的想法