2011-04-12 56 views
1

使用經典ASP對返回HTML的asp頁面進行JQuery ajax調用。這工作正常。現在我想不僅返回HTML,還返回一些數字,例如記錄數,從我打電話的頁面返回。我怎麼做?jquery classic asp返回HTML和記錄數/其他數據

這是我的AJAX調用:

function sendCcbRequest(text) { 
var jsonToSend = { text: escape(text) }; 
var jsonToSend={ id: 1, lastname: escape(text) } ; 

$.ajax({ 
    type: "POST", 
    url: 'test-ajax-handler.asp', 
    data: jsonToSend, 
    success: function(response) { 
     $('#output').append(response); 
    }, 
    error: function() { 
     alert("error"); 
    } 
}); // end ajax 

}

,它要求只是RESPONSE.WRITE的一些HTML頁面。我希望它也推出一個計數。

回答

1

將您的數據返回爲JSON:

{"html":"<div>This is your HTML<\/div","count":3} 


$.ajax({ 
    type: "POST", 
    url: 'test-ajax-handler.asp', 
    data: jsonToSend, 
    dataType: "json", 
    success: function(response) { 
     $('#output').append(response.html); 
     $('#count').html(response.count); 
    }, 
    error: function() { 
     alert("error"); 
    } 
}); 
2

你可以有你的ASP頁面發送自定義HTTP頭中,如響應:

XHR-Count: 5 

有:

Response.AddHeader "XHR-Count", "5" 

,然後取出在success回調這個頭:

$.ajax({ 
    type: "POST", 
    url: 'test-ajax-handler.asp', 
    data: jsonToSend, 
    success: function(response, status, xhr) { 
     var count = xhr.getResponseHeader('XHR-Count'); 
     alert(count); 
     $('#output').append(response); 
    }, 
    error: function() { 
     alert("error"); 
    } 
});