我正在尝试熟悉ASP .net,所以在'hello world'之后我试图添加一些简单的后端请求处理。所以我正在从我的页面到asp页面做一个Ajax请求:
Ext.Ajax.request({
type: 'POST',
url: 'http://http://localhost:49852/Default.aspx',
params: {
html : {'array': document.body.innerHTML}
},
success : function(response){
console.log('RESPONSE !', response);
//me.onSuccess(response, callback, errback);
},
failure : function(response){
console.log('RESPONSE FAIL !', response);
}
});
这是我的页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
它背后的代码(我不确定结构是否应该像这样,但我无法找到任何没有使用表单的请求处理示例):
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e){
if (this.Request["html"] != null){
this.Response.Write("{'success': true, 'msg': 'Its working'}");
}
else{
this.Response.Write("{'success': false, 'msg': 'Error in request data.'}");
}
}
}
现在,如果我在浏览器中访问此地址,则会显示正确的(错误)文本。但是当我尝试使用XHR请求时,我根本无法在Firebug控制台中看到任何请求,而在Net选项卡中我得到了“OPTIONS”响应:
在登录到控制台时看起来如下:
这里有什么想法?
答案 0 :(得分:2)
您的网址错误。它是:
http://http://localhost:49852/Default.aspx
应该是:
http://localhost:49852/Default.aspx
答案 1 :(得分:1)
尝试使用标有WebService
的SciptService
代替Page。
以下是MSDN的一个示例:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class SimpleWebService : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public string GetServerTime()
{
string serverTime =
String.Format("The current time is {0}.", DateTime.Now);
return serverTime;
}
}