2

我正在尝试熟悉 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 控制台中根本看不到任何请求,并且在“网络”选项卡中我得到“选项”响应:

在此处输入图像描述

登录到控制台时如下所示:

在此处输入图像描述

有什么想法吗?

4

2 回答 2

2

你的网址是错误的。这是:

http://http://localhost:49852/Default.aspx

什么时候应该:

http://localhost:49852/Default.aspx
于 2012-12-20T22:29:11.947 回答
1

尝试使用WebService标记为SciptService而不是页面。

这是来自 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;
    }
}
于 2012-12-21T14:29:05.697 回答