簡體   English   中英

使用Response.Write JSON輸出從Jquery調用c#asmx Web服務

[英]Calling c# asmx web service from Jquery with Response.Write JSON output

我有如下的ac#asmx Web服務代碼。

[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服務。 此網絡服務已在我的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); } 

可能是什么問題呢?

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

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

  1. 在catch塊中,使用文件處理代碼將該錯誤記錄到文本文件中。

  2. 如果您有權訪問在其中部署了服務並在該服務器上安裝了Visual Studio的服務器,請通過將其附加到w3wp進程來調試Web服務。

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

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

    在左側面板上,展開Windows日志,然后單擊應用程序菜單,然后在與您的應用程序相關的錯誤日志中找到詳細信息

public void getPolicyDetails(string CustId, string PolicyType)

您將CustID定義為字符串,但在json中為整數

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM