C# Ajax jquery 传递多个参数的 web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16556099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax jquery passing multiple parameters web services
提问by Carlo Adap
<script type="text/javascript">
$('#btnregister').click(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "fetchusers.asmx/RegUsers",
data: "{ username: '" + $("#txtuser").val() + "name: '" + $("#txtname").val() + "'}",
dataType: "json",
success: function (data) {
alert("Successfully register");
$("#btnregclose").click();
}
});
});
</script>
<div id="registration">
<fieldset>
<legend>Registration Form</legend>
<input id="txtuser" type="text" placeholder="Username" /><br />
<input id="txtname" type="text" placeholder="Name" /><br />
<input id="txtpass" type="password" placeholder="password" /><br />
<input id="txtconfirmpass" type="password" placeholder="confirm password" /><br />
<input id="btnregister" type="button" value="Register" />
<input id="btnregclose" type="button" value="close" />
</fieldset>
</div>
[WebMethod]
public string RegUsers(string username, string name)
{
string response = username + name;
return response;
}
I'm a beginner in Ajax Jquery and I'm doing exercise to improve my knowledge about it. My problem is when I click #btnregister it's not working. I think there's a problem in the parameters I passed on the ajax but I don't know what is it.
我是 Ajax Jquery 的初学者,我正在做练习以提高我对它的了解。我的问题是当我单击 #btnregister 时它不起作用。我认为我在ajax上传递的参数有问题,但我不知道它是什么。
采纳答案by Gavin Fang
try this :
尝试这个 :
$(document).ready(function () {
$('#btnregister').click(function () {
var obj = { username: $("#txtuser").val(), name: $("#txtname").val() };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "fetchusers.asmx/RegUsers",
data: JSON.stringify(obj),
dataType: "json",
success: function (data) {
alert("Successfully register");
$("#btnregclose").click();
}
});
});
});
this worked in my local enviroment.
这在我当地的环境中有效。
回答by felix Antony
use this syntax....
使用这种语法....
data: "{ 'username': '" + $("#txtuser").val() + "', 'name': '" + $("#txtname").val() + "'}",
回答by computrius
Instead of trying to build the string by concatination it might be easier to do something like this:
与其尝试通过串联来构建字符串,不如执行以下操作:
$.ajax(url, {
data: JSON.stringify({
username: $("#txtuser").val(),
name: $("#txtname).val()
})
});
It will prevent typos/problems that might occur if you have say, a comma, in one of your fields. Note though, that ie7 and lower will require you to include a file called json2.js (github).
如果您在其中一个字段中说逗号,它将防止可能出现的拼写错误/问题。但请注意,ie7 及更低版本将要求您包含一个名为 json2.js ( github)的文件。
Edit: Also, try executing your web service manually (just browse to the url, use a poster, etc). It is entirely possible you are getting a 404, or a server error.
编辑:另外,尝试手动执行您的网络服务(只需浏览到 url,使用海报等)。您完全有可能收到 404 或服务器错误。
Edit part 2: A good way to debug ajax issues in firefox is to use ctrl-shift-k to open the web console. Make sure "Net" is enabled and that "Log Request and Response Bodies" is checked in its dropdown. This way you can see the requests going out and coming back. If you dont see one then its an issue with your javascript, not the ajax.
编辑第 2 部分:在 firefox 中调试 ajax 问题的一个好方法是使用 ctrl-shift-k 打开 Web 控制台。确保启用“网络”,并在其下拉列表中选中“日志请求和响应机构”。通过这种方式,您可以看到请求发出和返回。如果你没有看到,那么它是你的 javascript 的问题,而不是 ajax。
Another Edit: Also, I see your click event isnt in a $(document).ready(function() {}); It could be that you are attaching the click event before the button is rendered. Therefore the event isnt attached and your not even executing the ajax code.
另一个编辑:另外,我看到您的点击事件不在 $(document).ready(function() {}); 中。可能是您在呈现按钮之前附加了单击事件。因此事件没有附加,你甚至没有执行 ajax 代码。
回答by Usman
The code in the question is right it needs simple solution. Go to yourwebservice.asmx.cs file and uncomment the following line given at class level this will resolve the issue of calling this webservice from jQuery or Ajax.
问题中的代码是正确的,它需要简单的解决方案。转到 yourwebservice.asmx.cs 文件并取消注释在类级别给出的以下行,这将解决从 jQuery 或 Ajax 调用此 Web 服务的问题。
[System.Web.Script.Services.ScriptService]

