Asmx从Extjs接收POSTed值(params)? [英] Asmx receiving POST -ed values(params) from Extjs?
问题描述
如何将已发布的值接收到从extjs发布的.asmx中的变量,因此可以使用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?
发送DATA WITH 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 reports error:
>
如何正确接收.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方法似乎没有任何争议。此外,您的方法期望返回一个字符串,但您不返回任何内容。所以要么返回一些东西,要么将你的方法签名修改为 void
。另外您已经设置了 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从Extjs接收POSTed值(params)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!