我遇到了一个执行`AJAX请求的问题。
指定contentType
和dataType
时,success
部分未执行。
但是,当省略它时,它正在执行,但显示生成的html
页面的整个内容。
这是我的AJAX电话:
$.ajax({
type: "POST",
url: "Default.aspx/GeneratePdfs",
data: '{frequency: "' + $('#ddlFrequency option:selected').text() + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$('#rptDisplay').text(data);
alert("1");
},
failure: function () {
// $('#rptDisplay').text("Error");
alert("2");
}
});
这是code behind
:
[System.Web.Services.WebMethod]
public static void GeneratePdfs(string frequency)
{
string test = frequency;
HttpResponse response = HttpContext.Current.Response;
response.Write(test);
}
这是html
页面的片段:
<div id="rptDisplay" class="well" runat="server" clientidmode="Static">
</div>
我需要在Web Method
部分显示从div
返回的数据。
我做错了什么?
答案 0 :(得分:1)
希望这会对你有所帮助:
contentType :将数据发送到服务器时。
dataType:您期望从服务器返回的数据类型。如果没有指定,jQuery将尝试根据响应的MIME类型推断它。
请找到代码:
[System.Web.Services.WebMethod]
public static string GeneratePdfs(string frequency)
{
string test = "frequency";//Hard Code value
HttpResponse response = HttpContext.Current.Response;
return test;
}
Design:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<div id="rptDisplay" class="well" runat="server" clientidmode="Static">
</div>
<script src="Scripts/jquery-1.7.1.min.js"></script>
<script src="Scripts/jquery-ui-1.8.20.min.js"></script>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "Default.aspx/GeneratePdfs",
data: '{frequency: "' + $('#ddlFrequency option:selected').text() + '" }',
contentType: "application/json;charset=utf-8",
// dataType: "text/Json",
success: function (data) {
debugger;
if (data.d!="") {
$('#rptDisplay').text(data.d);
}
alert("1");
},
failure: function () {
// $('#rptDisplay').text("Error");
alert("2");
}
});
</script>