weixin_33717117 2017-01-14 05:28 采纳率: 0%
浏览 46

Ajax转WCF服务

I am having a WCF Service (Ajax-enabled) and I need to call its methods using AJAX. The WCF Service name is NameService.svc and it is located within a WebService folder at the root directory of the Website. 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. 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. Is there a problem using the button element instead of input element with type as submit? What have I missed in the url of the ajax call? Should the method within the WCF Service be decorated with specific attributes? 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.

</div>
  • 写回答

1条回答 默认 最新

  • larry*wei 2017-01-14 05:37
    关注

    If you are doing $.ajax for http post and get. I will recommend you watch the tutorial from 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. 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. I am struck at there also... sighh

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。