ASMX接收POST -ed从ExtJS的值(PARAMS)? [英] Asmx receiving POST -ed values(params) from Extjs?

查看:127
本文介绍了ASMX接收POST -ed从ExtJS的值(PARAMS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何接收贴值到的.asmx变量是从ExtJS的发布,因此它可以使用ado.net数据库保存?

How to receive posted values to variable in .asmx that was posted from extjs, so it can be saved using ado.net to database?

发送数据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 }


            });
        }
    }
}


当submited,萤火报告错误:


如何正确接受她在的.asmx这个值,以便他们可以在[WebMethod的]使用和保存Ado.Net?


When submited, firebug reports error:


How to properly recieve this values in .asmx so they can be used in [WebMethod] and saved with 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内部服务器错误:

No. stil 500 Internal Server Error:



推荐答案

您的Web方法似乎并没有采取任何说法。此外,您的方法需要返回一个字符串,但你不返回任何东西。因此,无论是返回的东西或修改方法签名无效。您还可以设置 UseHttpGet = TRUE ,但在发送POST请求。尝试是这样的:

Your web method doesn't seem to take any argument. Also your method expects to return a string and yet you do not return anything. So either return something or modify your method signature to void. Also you have set UseHttpGet = true and yet you are sending a POST request. Try like this:

[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();
        }
    }       
}

此外,由于您已经定义了代理模式的Id属性作为一个非空整型我会建议你发送一个值是:

Also since you have defined the Id property of your Agents model as a non-nullable integer I would recommend you sending a value for it:

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

这篇关于ASMX接收POST -ed从ExtJS的值(PARAMS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆