[英]Issue with jQuery.ajax call to web service
I am Developing a Registration Form in ASP.NET in Which I am using client side scripting. 我正在使用客户端脚本在ASP.NET中开发注册表。 So I used jQuery.ajax method to POST all fields' data to server through a ASP.NET Web-Service.
因此,我使用jQuery.ajax方法通过ASP.NET Web服务将所有字段的数据发布到服务器。 But when I execute the Jquery.ajax method it calls error function.
但是,当我执行Jquery.ajax方法时,它将调用错误函数。 I tried to debug it using Firebug in Firefox.
我试图在Firefox中使用Firebug对其进行调试。 It showed error 500 Internal Server Error.
它显示错误500 Internal Server Error。 Now what I assume is that to Insert Data into a WebService I need to use a POST method and similarly I need a function on Server Side inside Webservice which can be invoked using POST method.
现在,我假设要将数据插入WebService中,我需要使用POST方法,并且类似地,我需要在Web Service中的Server Side上有一个可以使用POST方法调用的函数。 I am providing few snippets of code to illustrate what I have done.
我提供了一些代码片段来说明我所做的事情。
My client script: 我的客户脚本:
//My Client Side Function
function registerUser()
{
var User = {
FirstName:$get("txtFirstName").value,
LastName:$get("txtLastName").value
.
.
//and so on....This way I creat my json object to POST on server
};
//jQuery to POST Data
jQuery.ajax({
type:"POST",
url:"Service/UserRegistration.asmx/InsertUser",
data:User,
success:notifyUser,
error:setErrorField
});
}
//Function for Success Complition
function notifyUser(msg)
{
alert("Registration Successfull");
}
My web Service on the server: 服务器上的我的Web服务:
//My C# Web Service Logic
[WebMethod]
public void InsertUser(User use)
{
FreeUser us = new FreeUser();
us.FirstName = use.FirstName;
.
.
//and so on
}
Now what I suppose is that POST method requires WebInvoke
class as attribute. 现在,我想的是POST方法需要
WebInvoke
类作为属性。 How do I make a method which can be called by JavaScript? 如何制作可被JavaScript调用的方法? I may have to use
WebInvoke
for this. 我可能需要为此使用
WebInvoke
。
If you have ASP.NET AJAX available on the server-side (ASP.NET 2.0 + ASP.NET AJAX Extensions v1.0 or ASP.NET 3.5+), I've found that the easiest way to do it is to use JSON between the client and server-side. 如果服务器端有ASP.NET AJAX(ASP.NET 2.0 + ASP.NET AJAX扩展v1.0或ASP.NET 3.5+),我发现最简单的方法是使用JSON在客户端和服务器端之间。 You'll just need to add the [ScriptService] attribute to your service and then use this form for calling the service:
您只需要向您的服务添加[ScriptService]属性,然后使用以下形式调用该服务:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebService.asmx/WebMethodName",
data: "{}",
dataType: "json"
});
For more info, see the full post here: http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ 有关更多信息,请参见此处的完整文章: http : //encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/
In order to provide a JavaScript object as you are in that example code, you'll also need to use JSON.Stringify to generate an appropriate JSON string to pass to the server-side . 为了像在该示例代码中一样提供JavaScript对象,还需要使用JSON.Stringify生成适当的JSON字符串以传递到服务器端 。
如果要提交JSON对象,请在$.ajax
添加以下选项:
contentType: "application/json; charset=utf-8",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.