I use django 1.4 run on ubutu. I'm trying to use jquery/ajax to display data returned from a django method.
my file views.py
def json_page(request):
to_json = {
"key1" : "value1",
"key2" : "value2"
}
return HttpResponse(simplejson.dumps(to_json), mimetype="application/json;charset=UTF-8")
my html file:
<script>
$('document').ready(function() {
var url = "http://192.168.1.10:8000/json/";
$.ajax({
url: url,
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: false,
success: function(data) {
alert(data);
},
error: function(xhr, textStatus) {
console.log("error..");
}
});
});
</script>
I run file html, fire bug return status = 200 (ok). But not return data, return case: error
Could you help me fix it. Thanks all,
try this code.
<script>
$('document').ready(function() {
var url ="http://192.168.1.10:8000/json/";
$.ajax({
url: url,
type: "GET",
dataType: "json",
success: function(data) {
alert(success);
dat1=data.key1;
alert(dat1)
},
error: function(xhr, textStatus) {
console.log("error..");
}
});
});
</script>
in your view
def json_page(request):
to_json = {
"key1" : "value1",
"key2" : "value2"
}
data=json.dumps(to_json)
return HttpResponse(data, mimetype="application/json)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.