Asmx从Extjs接收POST值(params)?

时间:2011-10-23 16:32:24

标签: web-services post extjs ado.net asmx

如何接收从extjs发布的.asmx变量的发布值,以便可以使用ado.net保存到数据库?

使用EXT.AJAX发送数据

{
    text: 'Add',
    formBind: true,
    disabled: true,
    handler: function () {

        var form = this.up('form').getForm();
        var formValues = form.getValues();

        var firstName = formValues.firstName;
        var lastName = formValues.lastName;

        if (form.isValid()) {

            Ext.Ajax.request({

                url: 'WebServices/WebService.asmx/AddAgent',
                headers: { 'Content-Type': 'application/json' },
                method: 'POST',
                jsonData: { FirstName: firstName, LastName: lastName }


            });
        }
    }
}


当提交时,萤火虫报告错误:
enter image description here

如何在.asmx中正确接收这些值,以便它们可以在[WebMethod]中使用并与Ado.Net一起保存?

    [Serializable]
    public class Agents
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }


    //CREATE
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]

    public string AddAgent()
    {
        string connStr = ConfigurationManager.ConnectionStrings["AgentsServices"].ConnectionString; 

        using (SqlConnection connection = new SqlConnection(connStr))
        {
            connection.Open();

            using (SqlCommand command = new SqlCommand("insert into Agent(id, firstName, lastName) values(@id, @firstName, @lastName)", connection))
            {
                command.Parameters.AddWithValue("@firstName", FirstName); //here i get message( The name "FirstNAme does not exist in current context")
                command.Parameters.AddWithValue("@lastName", LastName); // -||-

                command.ExecuteNonQuery();

            }
        }       
    } 

编辑:

没有。 stil 500内部服务器错误:


enter image description here

1 个答案:

答案 0 :(得分:3)

您的网络方法似乎没有任何争论。你的方法也希望返回一个字符串,但你不返回任何东西。因此,要么返回一些内容,要么将方法签名修改为void。此外,您已设置UseHttpGet = true,但您正在发送POST请求。试试这样:

[WebMethod]
[ScriptMethod]
public void AddAgent(Agents agents)
{
    string connStr = ConfigurationManager.ConnectionStrings["AgentsServices"].ConnectionString; 
    using (SqlConnection connection = new SqlConnection(connStr))
    {
        connection.Open();
        using (SqlCommand command = new SqlCommand("insert into Agent(id, firstName, lastName) values(@id, @firstName, @lastName)", connection))
        {
            command.Parameters.AddWithValue("@firstName", agents.FirstName);
            command.Parameters.AddWithValue("@lastName", agents.LastName);
            command.ExecuteNonQuery();
        }
    }       
}

此外,由于您已将Agents模型的Id属性定义为不可为空的整数,我建议您为其发送一个值:

jsonData: { agents: { Id: 0, FirstName: firstName, LastName: lastName } }