3
假设我有两个动作的方法action1
和action2
:返回对象和响应返回的区别?
措施1:
public JavaScriptSerializer action1()
{
var student = new Student() { First = "john", Last = "doe" };
JavaScriptSerializer jsonStudent = new JavaScriptSerializer();
jsonStudent.Serialize(student);
return jsonStudent;
}
措施2:
public void action2()
{
var student = new Student() { First = "john", Last = "doe" };
JavaScriptSerializer jsonStudent = new JavaScriptSerializer();
jsonStudent.Serialize(student);
Response.Write(jsonStudent);
}
假设我的看法有Ajax
调用是这样的:
<script>
$(function() {
$.ajax({
url: 'AjaxCallsTest/action1',
dataType: 'json',
success: function (response) {
//code here
},
error: function (response, status, xhr) {
//code here
}
})
})
</script>
在两种情况下,一个写入Response
对象,另一个写入return
声明。我的问题,即使有return
,它实际上是否将jsonStudent
对象添加到Response
对象,如果是这样,使用return
语句写入操作方法是毫无意义的?
感谢。
通过Response.Write()写入Response的outputStream和打印到Response之间有什么区别? – cptmemo
老实说,我不确定,但那可能有帮助:http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx –