import json
from django.http import HttpResponse
def get_mydata(request):
response = dict()
response['status'] = 'success'
response['msg'] = 'hello, CJ'
.... # more key-value pair as you need
return HttpResponse(json.dumps(response), content_type="application/json")
你知道吗网址.py地址:
from django.conf.urls import patterns
from django.conf.urls import url
import views
urlpatterns = patterns('',
url(r'^get_data/', views.get_mydata, name='get_mydata')
)
html/javascript:
$.ajax({
"type": "GET",
"dataType": "json",
"url": "/get_data/",
"success": function(result) {
console.log(result); // here you get the json response from get_mydata() in views.py
}
})
您可能希望使用
json.dumps()
来帮助您将字典转换为json,以及在django.http
中使用HttpResponse
。 一个粗略的例子是:你知道吗视图.py地址:
你知道吗网址.py地址:
html/javascript:
相关问题 更多 >
编程相关推荐