【发布时间】:2011-10-27 04:09:24
【问题描述】:
这是我的代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="<%= ResolveUrl("Scripts/jquery-1.3.2.min.js") %>"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="btn" value= "call web service" onclick="callwebservice()" />
</div>
</form>
</body>
</html>
<script type="text/javascript" language="javascript">
function AjaxFailed(result) {
alert('call stastus:' + result.status + ' ' + result.statusText);
alert('responsetest"'+result.responseText);
alert('errorthrown' + result.errorThrown);
}
function callwebservice() {
$.ajax(
{
type: "POST",
url: "default.aspx/LoginFromFacebook",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
alert(response);
},
error: AjaxFailed
}
);
}
</script>
code behind -----------------
[WebMethod()]
public static string LoginFromFacebook()
{
return "helloworld";
}
这是非常简单的代码,当我调用 webmethod 时,我的 default.aspx 文件中只有一个 web 方法,我得到响应代码 200 ok。 我调查使用提琴手 响应如下 HTTP/1.1 200 正常 缓存控制:私有 内容类型:文本/html;字符集=utf-8 服务器:Microsoft-IIS/7.5 X-AspNet-版本:2.0.50727 X-Powered-By: ASP.NET 日期:2011 年 8 月 15 日星期一 09:16:21 GMT 内容长度:1535
不知道为什么会这样。我只期望字符串作为响应,而我将整个页面的 html 作为响应。
【问题讨论】:
-
你有没有看过服务器发回的响应。是你好世界吗?你可以使用firebug来检查。
-
如果返回 {"text":"helloworld"}?你试过了吗?
-
不,我没有得到 hello world 作为响应。相反,我得到了 default.aspx 的 html
标签: ajax