Ajax 无法将JSON数据从Django发送到javascript
我需要将Ajax 无法将JSON数据从Django发送到javascript,ajax,django,Ajax,Django,我需要将html文件中的一些数据发送到django进行一些处理,然后使用ajax 但是响应转到ajax调用中的错误回调,状态为0 这是ajax代码,我已经尝试了两个注释URL function snapshot() { ctx.drawImage(video, 0,0, canvas.width, canvas.height); imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
html
文件中的一些数据发送到django
进行一些处理,然后使用ajax
但是响应转到ajax调用中的错误回调,状态为0
这是ajax代码,我已经尝试了两个注释URL
function snapshot() {
ctx.drawImage(video, 0,0, canvas.width, canvas.height);
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
console.log(imageData)
//url:"{% url 'blind:getcaption' %}",
$.ajax({
type: 'GET',
url: 'http://127.0.0.1:8000/blind/ajax/getcaption/',
//url: /ajax/getcaption/
data: {
'img': imageData
},
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function(request, status, error){
console.log(request, " " ,status, " ", error)
}
});
}
这是views.py
def getcaption(request):
img = request.GET.get('img', None)
data = {'caption': "This caption for test"}
return JsonResponse(data)
和url.py
app_name = 'blind'
urlpatterns = [
path('', views.index, name='index'),
url(r'^ajax/getcaption/$', views.getcaption, name='getcaption'),
]
但当我输入url时,点击
url:'http://127.0.0.1:8000/blind/ajax/getcaption/
我在chrome中重新查看json对象首先,您可以在ajax调用中编写url
,如下图所示,然后解析您获得成功的json
function snapshot() {
ctx.drawImage(video, 0,0, canvas.width, canvas.height);
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
console.log(imageData)
//url:"{% url 'blind:getcaption' %}",
$.ajax({
type: 'GET',
url : '{% url "getcaption" %}',
data: {
'img': imageData
},
dataType: 'json',
success: function (data) {
console.log(JSON.parse(data));
},
error: function(request, status, error){
console.log(request, " " ,status, " ", error)
}
});
}
然后更改视图,在返回json响应之前将字典转储到json对象:
def getcaption(request):
img = request.GET.get('img', None)
data = {'caption': "This caption for test"}
return JsonResponse(data)
首先,您可以在ajax调用中编写
url
,如下图所示,然后解析成功的JSON
function snapshot() {
ctx.drawImage(video, 0,0, canvas.width, canvas.height);
imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
console.log(imageData)
//url:"{% url 'blind:getcaption' %}",
$.ajax({
type: 'GET',
url : '{% url "getcaption" %}',
data: {
'img': imageData
},
dataType: 'json',
success: function (data) {
console.log(JSON.parse(data));
},
error: function(request, status, error){
console.log(request, " " ,status, " ", error)
}
});
}
然后更改视图,在返回json响应之前将字典转储到json对象:
def getcaption(request):
img = request.GET.get('img', None)
data = {'caption': "This caption for test"}
return JsonResponse(data)
看看我在这里的答案:也将
xhr
作为错误函数中的第一个参数传递,然后传递console.log(xhr.status);console.log(xhr.responseText)
获取详细错误消息。url如何?我应该如何输入,以匹配您的答案!就是这样:url:'/blind/getcaption/'
如果你认为我的答案对未来的读者会有帮助,那么别忘了投票:)我在使用console.log(xhr.responseText)
时没有定义我的答案:在这里看一下我的答案:也将xhr
作为错误函数中的第一个参数传递,然后再传递console.log(xhr.status);console.log(xhr.responseText)
以获取详细的错误消息。url如何?我应该如何键入它,以匹配您的答案!只需这样:url:'/blind/getcaption/'
如果您认为我的答案对未来的读者有帮助,请不要忘记对其进行投票:)我使用console.log时未定义(xhr.responseText)
使用JsonResponse
时不需要转储数据,因为这是自动完成的。因此我编辑了答案。使用JsonResponse
时不需要转储数据,因为这是自动完成的。因此我编辑了答案。