gpt4 book ai didi

web-services - Asmx 从 Extjs 接收 POST -ed 值(参数)?

转载 作者:行者123 更新时间:2023-12-04 06:10:08 24 4
gpt4 key购买 nike

如何接收从 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 }


});
}
}
}

提交时,firebug 报告错误:
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

最佳答案

您的网络方法似乎没有任何论据。此外,您的方法期望返回一个字符串,但您不返回任何内容。因此,要么返回某些内容,要么将您的方法签名修改为 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 } }

关于web-services - Asmx 从 Extjs 接收 POST -ed 值(参数)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com