AJAX - "Success" 指定 contentType 和 dataType 时不调用操作
AJAX - "Success" action is not invoked when specifying contentType and dataType
我 运行 在执行 `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>
我需要在 div
部分显示从 Web Method
返回的数据。
我做错了什么?
希望对您有所帮助:
contentType :向服务器发送数据时。
dataType: 您期望从服务器返回的数据类型。如果指定 none,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>
我 运行 在执行 `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>
我需要在 div
部分显示从 Web Method
返回的数据。
我做错了什么?
希望对您有所帮助:
contentType :向服务器发送数据时。
dataType: 您期望从服务器返回的数据类型。如果指定 none,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>