我想在数据库的表单文本框中插入成员的规范。当我使用静态值调用 web 方法时,我使用 jQuery Ajax 执行此操作。操作成功。例如,这段代码是可以的:
$.ajax({
type: "POST",
url:"MethodInvokeWithJQuery.aspx/executeinsert",
data: '{ "username": "user1", "name":"john","family":"michael","password":"123456","email": "email@yahoo.com", "tel": "123456", "codemeli": "123" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
$('#myDiv2').text(msg.d);
},
error: function (x, e) {
alert("The call to the server side failed. " + x.responseText);
}
}
);
但是当我想使用动态输入到文本框中的值时,就会发生错误。什么问题?我尝试了以下两个代码块。
<script type="text/javascript">
$(document).ready(
function () {
$("#Button1").click(
function () {
var username, family, name, email, tel, codemeli, password;
username = $('#<%=TextBox1.ClientID%>').val();
name = $('#<%=TextBox2.ClientID%>').val();
family = $('#<%=TextBox3.ClientID%>').val();
password = $('#<%=TextBox4.ClientID%>').val();
email = $('#<%=TextBox5.ClientID%>').val();
tel = $('#<%=TextBox6.ClientID%>').val();
codemeli = $('#<%=TextBox7.ClientID%>').val();
$.ajax(
{
type: "POST",
url: "WebApplication20.aspx/executeinsert",
data: "{'username':'username','name':name,
'family':family,'password':password,
'email':email,'tel':tel,
'codemeli':codemeli}",
contentType: "application/json;charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
alert(msg);
},
error: function (x, e) {
alert("The call to the server side failed. "
+ x.responseText);
}
}
);
}
)
})
</script>
和
$(document).ready(
function () {
$("#Button1").click(
function () {
var username, family, name, email, tel, codemeli, password;
username = $('#<%=TextBox1.ClientID%>').val();
name = $('#<%=TextBox2.ClientID%>').val();
family = $('#<%=TextBox3.ClientID%>').val();
password = $('#<%=TextBox4.ClientID%>').val();
email = $('#<%=TextBox5.ClientID%>').val();
tel = $('#<%=TextBox6.ClientID%>').val();
codemeli = $('#<%=TextBox7.ClientID%>').val();
$.ajax(
{
type: "POST",
url: "WebApplication20.aspx/executeinsert",
data: '{"username" : '+username+', "name": '+name+', "family": '+family+',
"password": '+password+', "email": '+email+', "tel": '+tel+' ,
"codemeli": '+codemeli+'}'
contentType: "application/json;charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function(msg) {
alert(msg);
},
error: function (x, e) {
alert("The call to the server side failed. "
+ x.responseText);
}
}
);
}
)
})
这里的错误内容是:
{"Message":"Invalid JSON primitive: myname.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)
at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)
at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)
at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)
at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}