我正在调用jquery ajax到下面给出的页面
<script type="text/javascript">
function Showgrid() {
$.ajax({
type: "GET",
url: "popup.aspx",
contentType: "application/json; charset=utf-8",
data: {locale: 'en-US' },
dataType: "json",
success: function (data) {
$("#target").html(data.d);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
</script>
在popup.aspx页面加载上,我以的形式编写了代码
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/plain";
Response.Write(Request.QueryString[0]);
Response.Write(DateTime.Now.ToString());
Response.End();
}
我得到了回应,但不是成功方法,而是错误函数请提出错误的
更改为POST
,即type: "POST"
。在页面中转到Response.Write(Request[0]);
.aspx页面中的输出不是json类型。在写入响应之前使用Json编码或更改数据类型:text
如前所述,将类型更改为POST并用引号括起数据。在服务器端,您需要调用一个web方法。不能使用page_load,因为您正在传递参数"locale"。请参阅下面修改后的JSON函数和服务器代码(假设您使用的服务器端代码是正确的):
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static void ShowGrid(string locale)
{
HttpContext.Current.Response.ContentType = "text/plain";
HttpContext.Current.Response.Write(HttpContext.Current.Request.QueryString[0]);
HttpContext.Current.Response.Write(DateTime.Now.ToString());
HttpContext.Current.Response.End();
}
JSON:
function Showgrid() {
$.ajax({
type: "POST",
url: "popup.aspx/ShowGrid",
contentType: "application/json; charset=utf-8",
data: "{ 'locale': 'en-US' }",
dataType: "text",
success: function (data) {
$("#target").html(data.d);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}