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 –