2016-11-22 130 views
0

我有一個如下所示的c#asmx web服務代碼。從Response.Write調用c#asmx web服務使用Response.Write JSON輸出

[WebMethod] 
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] 
    public void getPolicyDetails(string CustId, string PolicyType) 
    { 
     JavaScriptSerializer js = new JavaScriptSerializer(); 
     Context.Response.Clear(); 
     Context.Response.ContentType = "application/json"; 
     clsPolicy data = new clsPolicy(); 
     try 
     { 
      string policyTxt = ""; 
      //get PolicyTxt from Database 
      data.Message = policyTxt.ToString(); 
     } 
     catch (Exception ex) 
     { 
     } 
     Context.Response.Write(js.Serialize(data)); 
    } 

現在,我試圖在asp.net中使用此Web服務。這個web服務已經成功地在我的android項目中使用了。

當我嘗試使用通過JavaScript調用它,它給了我一個500 - 內部服務器錯誤。以下是我的Javascript代碼。

function getPolicy(policyType) { 
 
      $.ajax({ 
 
       type: "POST", 
 
       url: "http://myurl/productsearch.asmx/getPolicyDetails", 
 
       data: '{CustId: 106206,PolicyType: "' + policyType + '" }', 
 
       contentType: "application/json; charset=utf-8", 
 
       dataType: "json", 
 
       success: OnSuccess, 
 
       failure: function (response) { 
 
        alert(response.d); 
 
       } 
 
      }); 
 
     } 
 
     function OnSuccess(response) { 
 
      alert(response.d); 
 
     }

可能是什麼問題呢?

回答

0
public void getPolicyDetails(string CustId, string PolicyType) 

您可以定義客戶ID爲字符串,但在你的JSON是整數

data: '{CustId: 106206,PolicyType: "' + policyType + '" }' 

也開始使用JSON.stringify爲您的數據的創建。

var policyType = 'something'; 
var custID = '106206'; 
data: JSON.stringify({CustId:custID, PolicyType: policyType }) 
+0

好的。讓我嘗試一個字符串並回復你。 –

+0

修改代碼:var custid =「106206」; data:'{CustId:''+ custid +'「,PolicyType:」'+ policyType +'「}'。 –

+0

它仍然是500內部服務器錯誤。 –

1

錯誤500內部服務器錯誤與web服務中的錯誤有關。

不同的方法來調試/記錄錯誤

  1. 在catch塊使用文件處理代碼日誌錯誤到文本文件中。

  2. 如果您有權訪問部署了服務的服務器並在該服務器上安裝了visual studio,請將其附加到w3wp進程中,以調試您的webservice。

  3. 您可以檢查錯誤Windows事件查看器

    控制面板 - >管理工具 - >事件查看器

    在左側面板中展開Windows日誌,並點擊應用程序菜單中,找到錯誤的細節與您的應用程序相關的日誌