简体   繁体   English

Ajax转WCF服务

[英]Ajax to WCF Service

I am having a WCF Service (Ajax-enabled) and I need to call its methods using AJAX. 我有一个WCF服务(启用Ajax),需要使用AJAX调用其方法。 The WCF Service name is NameService.svc and it is located within a WebService folder at the root directory of the Website. WCF服务名称为NameService.svc,位于网站根目录的WebService文件夹中。 Here is how things work: I have to create a user control which includes a table layout containing the text fields to collect the user data and when clicking the button I need to use jQuery and Ajax to call the methods which will insert the data to the database. 工作原理如下:我必须创建一个用户控件,其中包括一个表布局,该表布局包含用于收集用户数据的文本字段,当单击按钮时,我需要使用jQuery和Ajax来调用将数据插入到表中的方法。数据库。 I tried the following code but it did not work for me and I need to know what I am missing or wrote incorrectly within the user control: 我尝试了以下代码,但对我而言不起作用,并且我需要知道我在用户控件中缺少什么或写错了什么:

 $(function() { $('button').click(function() { var name = $('text').val(); $.ajax({ url : 'NameService.svc/InsertData', data : { suppliername : name }, method : 'post', datatype : 'json', success : function(data) { alert(data); } }); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <table class="table"> <tbody> <tr> <td>Supplier Name:</td> <td><input type="text" name="sname"></td> </tr> <td colspan="2"><button type="button" class="btn btn primary">Insert</button></td> </tbody> </table> 

Note I am using Bootstrap within my project. 注意我在项目中使用Bootstrap。 Is there a problem using the button element instead of input element with type as submit? 使用button元素而不是类型为Submit的输入元素是否存在问题? What have I missed in the url of the ajax call? 我在ajax调用的URL中错过了什么? Should the method within the WCF Service be decorated with specific attributes? WCF服务中的方法是否应使用特定属性修饰? If I will call a method that will return only data should I use the data property within the Ajax call or ignore it also the method will be post to or it should be get in that way. 如果我将调用仅返回数据的方法,则应该使用Ajax调用中的data属性,还是将其忽略,否则该方法将被发布到或应以这种方式获取。

If you are doing $.ajax for http post and get. 如果您正在使用$ .ajax进行http发布并获取。 I will recommend you watch the tutorial from https://www.udemy.com/rest-wcf-service-in-aspnet/learn/v4/t/lecture/5225772 我建议您从https://www.udemy.com/rest-wcf-service-in-aspnet/learn/v4/t/lecture/5225772观看本教程

After you retrieve the data, you can do a database connection from svc.cs and import these data(s) to your database. 检索数据后,可以从svc.cs建立数据库连接,并将这些数据导入数据库。 I assume you have already know how to do database connection. 我假设您已经知道如何进行数据库连接。 If no, just do some online search, you probably will learn that. 如果不是,只需进行一些在线搜索,您可能就会学到。

However, I have no clues if your data is in nested json format. 但是,我不知道您的数据是否为嵌套的json格式。 I am struck at there also... sighh 我也被那边打了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM