user5502499
user5502499

Reputation:

Ajax to WCF Service

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.

Upvotes: 0

Views: 93

Answers (1)

Wei Hao
Wei Hao

Reputation: 11

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

Upvotes: 1

Related Questions