这是我的帖子:

$("#checkin-button").click(function() { 
  var mid = $("input#mid").val();
  var dataString = 'mid='+mid;  
  $.ajax({  
    type: "POST",  
    url: "/game-checkin",  
    data: dataString,  
    success: function() {  
      $('#checkin-form').html("<div id='message'></div>");  
      $('#message').html("<h2>You are checked in!</h2>");  
    }  
  });  
return false;
});  

I am sending back a JSON string and want to use it in my Django template. How do I capture that response for display in the template?

我正在发送一个JSON字符串,并希望在Django模板中使用它。如何捕获该响应以在模板中显示?

3 个解决方案

#1


2  

From the API

从API

success(data, textStatus, XMLHttpRequest)Function

成功(数据、textStatus XMLHttpRequest)函数

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the 'dataType' parameter; a string describing the status; and the XMLHttpRequest object (available as of jQuery 1.4). This is an Ajax Event.

请求成功时要调用的函数。函数通过三个参数:从服务器返回的数据,根据“dataType”参数进行格式化;描述状态的字符串;以及XMLHttpRequest对象(在jQuery 1.4中可用)。这是一个Ajax事件。

I think you'll have to use parseJSON() to convert it.

我认为您必须使用parseJSON()来转换它。

#2


1  

You can simply do this :

你可以这么做:

$.post("/game-checkin", {mid: mid }, function(data){
  //response in json format. Example: data.something
}, "json");

#3


0  

Use this function as callback:

使用这个函数作为回调函数:

success: function(response) {
    alert(response);
}

The response will be what your server returns, but as text. If you have to use that JSON effectively in Javascript you have to use the eval() function like this: eval('(' + response ')'); and it will evaluate the JSON expression so you can assign it to an object like this:

响应将是服务器返回的内容,但作为文本。如果必须在Javascript中有效地使用JSON,就必须使用eval()函数:eval('(' (' + response ')');它会对JSON表达式求值,这样你就可以把它赋给这样的对象:

success: function(response) {
    var responseObj = eval('(' + response + ')');
    // use responseObj here
}

I don't know about Django, but this is how you get the response and evaluate it as JSON.

我不知道Django的情况,但是这就是如何获得响应并将其作为JSON进行计算的方法。

#1


2  

From the API

从API

success(data, textStatus, XMLHttpRequest)Function

成功(数据、textStatus XMLHttpRequest)函数

A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the 'dataType' parameter; a string describing the status; and the XMLHttpRequest object (available as of jQuery 1.4). This is an Ajax Event.

请求成功时要调用的函数。函数通过三个参数:从服务器返回的数据,根据“dataType”参数进行格式化;描述状态的字符串;以及XMLHttpRequest对象(在jQuery 1.4中可用)。这是一个Ajax事件。

I think you'll have to use parseJSON() to convert it.

我认为您必须使用parseJSON()来转换它。

#2


1  

You can simply do this :

你可以这么做:

$.post("/game-checkin", {mid: mid }, function(data){
  //response in json format. Example: data.something
}, "json");

#3


0  

Use this function as callback:

使用这个函数作为回调函数:

success: function(response) {
    alert(response);
}

The response will be what your server returns, but as text. If you have to use that JSON effectively in Javascript you have to use the eval() function like this: eval('(' + response ')'); and it will evaluate the JSON expression so you can assign it to an object like this:

响应将是服务器返回的内容,但作为文本。如果必须在Javascript中有效地使用JSON,就必须使用eval()函数:eval('(' (' + response ')');它会对JSON表达式求值,这样你就可以把它赋给这样的对象:

success: function(response) {
    var responseObj = eval('(' + response + ')');
    // use responseObj here
}

I don't know about Django, but this is how you get the response and evaluate it as JSON.

我不知道Django的情况,但是这就是如何获得响应并将其作为JSON进行计算的方法。

标签:

相关文章