0

jQueryを介してWebサービスを呼び出そうとしていますが、結果やエラーが表示されません。私のコードは次のとおりです。

<script type="text/javascript" language="javascript">
    var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript"> 
    alert(empId);    
    $.ajax({ 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json", 
        url: "ServiceGetUser.asmx/GetEmployee", 
        data: "{'employeeId': '" + empId + "'}", 
        success: function (data) { 
            alert("Employee name: " + data.d); 
        }, 
        error: function () { 
            alert("Error calling the web service.");  
        } 
    }); 
</script>

正常に印刷してからWebサービスに渡し、Webサービスコードに示されているようにsession名前を印刷することで値を取得しています。Jane Developer

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace MentorMentee
{
    /// <summary>
    /// Summary description for ServiceGetUser
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class ServiceGetUser : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetEmployee(string employeeId)
        {
            return "Jane Developer";
        }
    }
}

何が悪いのか、あなたの提案を期待しています

ありがとう

4

4 に答える 4

2

あなたはこれを入れることができますdoc ready block

$(window).load(function(){
   $.ajax({ 
      type: "POST", 
      dataType: "json", 
      contentType: "application/json", 
      url: "ServiceGetUser.asmx/GetEmployee", 
      data: {'employeeId': '<%= Session["UserName"] %>'}, //<-- try it
      success: function (data) { 
          alert("Employee name: " + data.d); 
      }, 
      error: function () { 
          alert("Error calling the web service.");  
      } 
    });
 });
于 2013-02-06T12:21:11.447 に答える
1

dataTypeはjsonとして指定されます。これは、jqueryがサーバーから受信した応答をjsonに解析することを意味します。しかし、あなたはあなたのサービスで文字列を返しています。したがって、解析エラーはjQueryによってスローされます。

コールバックを添付してみてくださいcomplete(jqXHR jqXHR, String textStatus)。そしてtextStatusを見てください

使ってみてください

<script type="text/javascript" language="javascript">
    var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript"> 
    alert(empId);    
    $.ajax({ 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json", 
        url: "ServiceGetUser.asmx/GetEmployee", 
        data: "{'employeeId': '" + empId + "'}", 
        success: function (data) { 
            alert("Employee name: " + data.d); 
        }, 
        error: function () { 
            alert("Error calling the web service.");  
        } ,
        complete: function(xhr, textStatus) {
            alert(textStatus);
        }
    }); 
</script>
于 2013-02-06T12:21:28.913 に答える
0

これを試して:

<script type="text/javascript" language="javascript">
    var empId = '<%= Session["UserName"] %>';
</script>
<script type="text/javascript"> 
    alert(empId);    
    $.ajax({ 
        type: "POST", 
        dataType: "json", 
        contentType: "application/json; charset=utf-8", 
        url: "ServiceGetUser.asmx/GetEmployee", 
        data: '{employeeId: "' + $("#<%=employeeId.ClientID%>")[0].value + '" }',
        success: function (data) { 
            alert("Employee name: " + data.d); 
        }, 
        error: function () { 
            alert("Error calling the web service.");  
        } 
    }); 
</script>
于 2013-02-06T12:36:45.613 に答える
0

最初にこのようにjsonを文字列化してみてください 。そうalert(JSON.stringify(data))すれば、間違いを見つけることができるかもしれません。

于 2013-02-06T12:37:15.113 に答える