2016-04-05 6 views
0

soapService.aspxという名前のc#で作成されたSOAP Webサービスがあります。これは、displayClass(String id、String theDate)というメソッドを持っています。返すのは です。これはjsonの後に戻ります2つのパラメータを送信するjquery ajaxを使用したメソッドへのSOAP webservice呼び出し、ループ内の応答の捕捉方法

 

[{"lesson_id":2,"customer_id":4,"instructor_id":8,"dance_style_id":2,"hourly_rate":20,"lesson_time":"Afternoon","lesson_date":"03/12/2015","start_time":"11:22","end_time":"14:22 ","notes":"test test","payment_status":0,"status":0,"lesson_slot":null,"duration":3},{"lesson_id":3,"customer_id":4,"instructor_id":8,"dance_style_id":2,"hourly_rate":20,"lesson_time":"Afternoon","lesson_date":"03/12/2015","start_time":null,"end_time":null,"notes":null,"payment_status":0,"status":0,"lesson_slot":null,"duration":3},{"lesson_id":4,"customer_id":4,"instructor_id":8,"dance_style_id":2,"hourly_rate":20,"lesson_time":"Afternoon","lesson_date":"03/12/2015","start_time":null,"end_time":null,"notes":null,"payment_status":0,"status":0,"lesson_slot":null,"duration":3}] 

これは、Webサービスの説明ページで指定されたWebサービスの呼び出しメソッドを使用して見つかりました。

私はajaxを使用して応答をキャッチしたいと思います。

は、これまでのところ私が書いたこの

 
$(document).ready(function() { 

    function displayClass() { 
     var instructorInputID = $('#instructorIdText').val(); 
     var instructorInputDate = $('#instructordateText').val(); 
     //send this id to web service 

     $.ajax({ 

      url: "http://localhost/soapService.asmx/displayClasses", 
      type: POST, 
      dataType:"json", 
      data:instructorInput, 
      contentType:"application/json; charset:utf-8", 

      success:function(msg){ 

       //process the msg 

      } 


     }); 

    } 

}); 

1)どのように私は、テーブル内のすべてのこれらのJSONデータを表示するにはどうすればよい渡すパラメータ 2)により、Webサービスメソッドを呼び出すのですか?

編集を助けてください:しようとしましたが、これは

 
XMLHttpRequest cannot load http://localhost:18324/soapService.asmx/displayClasses. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:17182' is therefore not allowed access.
EDIT Two : 
complete code : Still Error.. msg not defined 
<pre> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 

$("#findClassBtn").click(function() { 

displayClass(); 
}); 

function onSuccess(msg) { 
$.each(msg, function(i, item) { 
var tds = ""; 
$.each(item, function(i, item) { 
tds += "<td>" + item + "</td>"; 
}); 
$('#table').append("<tr>" + tds + "</tr>"); 
}); 
} 

function displayClass() { 
var instructorInputID = $('#instructorIdText').val(); 
var instructorInputDate = $('#instructordateText').val(); 
//send this id to web service 
$.ajax({ 
url: "soapService.asmx/displayClasses", 
type: "POST", 
dataType:"json", 
data: { 
'id': instructorInputID, 
'theDate': instructorInputDate 
}, 
contentType: "application/json; charset:utf-8", 
success: onSuccess(msg) 
}); 
} 

</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
</div> 
<asp:Label ID="Label1" runat="server" Text="Add Your Id"></asp:Label> 
<p> 
<asp:TextBox ID="instructorIdText" runat="server"></asp:TextBox> 
</p> 
<asp:Label ID="Label2" runat="server" Text="Add date (dd/mm/yyyy)"></asp:Label> 
<p> 
<asp:TextBox ID="instructordateText" runat="server"></asp:TextBox> 
</p> 
<asp:Button ID="findClassBtn" runat="server" OnClick="findClassBtn_Click" Text="Find Classes" /> 
<p> 
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> 
</p> 
</form> 
<table id="table"> 
</table> 
<pre> 

Error: msg is not defined. 
+0

が_data_では、あなたがしようとした私は、コンソールから取得応答である

 data: "{'id': '" + instructorInputID + "','theDate': '" + instructorInputDate + "'}", 

投稿しますjsonオブジェクトではなく文字列を投稿してください – Romario

+0

私は自分の答えを修正しました。 – Romario

答えて

0

$(document).ready(function() { 
 

 
    function displayClass() { 
 
     var instructorInputID = $('#instructorIdText').val(); 
 
     var instructorInputDate = $('#instructordateText').val(); 
 
     //send this id to web service 
 

 
     $.ajax({ 
 

 
      url: "http://localhost/soapService.asmx/displayClasses", 
 
      type: POST, 
 
      dataType:"json", 
 
      data: { 
 
       'id': instructorInputID, 
 
       'theDate': instructorInputDate 
 
      }, 
 
      contentType: "application/json; charset:utf-8", 
 

 
      success: function (msg) { 
 
       $.each(msg, function(i, item) { 
 
       var tds = ""; 
 
       $.each(item, function(i, item) { 
 
        tds += "<td>" + item + "</td>"; 
 
       }); 
 
       $('#table').append("<tr>" + tds + "</tr>"); 
 
       }); 
 
      } 
 
     }); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table id="table"> 
 
</table>

+0

コードを追加しました。それは内部500エラーを与える。 WebサービスASPXの場所はlocalhostにあり、クライアントコードもlocalhostにあります。これが問題の原因ですか? –

+0

@NurulAlamAnikこのエラーは? 'XMLHttpRequestはhttp:// localhost:18324/soapService.asmx/displayClassesをロードできません。プリフライト要求への応答がアクセス制御チェックを通過しない:要求されたリソースに「アクセス制御許可」がない。 Origin 'http:// localhost:17182'はアクセスが許可されていません。 – Romario

+0

@「http:// localhost:17182」から「http:// localhost:18324 /」にリクエストを送信しようとしているようです。 .. '。クロス・オリジン・リクエストです。このhttp://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resourceを確認してください。 – Romario

関連する問題