1 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個贊
該window.location命令使您的瀏覽器導(dǎo)航到給定 URL 指定的新位置。
而是在您希望注入部分內(nèi)容的頁面上選擇一些現(xiàn)有元素,并將innerHTML該元素的屬性設(shè)置為響應(yīng)的內(nèi)容。例如,假設(shè)您在某個地方有一個現(xiàn)有的 div:
<div id="results"></div>
然后,在您的 JavaScript 中,您可以執(zhí)行以下操作:
$(".callSearch").click(function (e) {
e.preventDefault();
var url = '@Url.Action("Search", "Search")';
$.ajax(
{
type: "GET",
url: 'Search/Search',
dataType: "html",
success: function (response) {
$("#results").html(response); //set the HTML content of the "results" div to be the response from the server, which contains the HTML generated by execution of the partial view
},
error: function () {
alert("Error");
}
});
});
注意,如果您在不同的 URL 和/或端口對另一個項(xiàng)目進(jìn)行 ajax 調(diào)用,您可能必須設(shè)置另一個項(xiàng)目以接受 CORS 請求。
- 1 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報