Вызов функции c # из исходного кода с использованием javascript

#javascript #c# #web-services #code-behind

#javascript #c# #веб-сервисы #code-behind

Вопрос:

Мне нужно вызвать функцию c # в моем исходном коде с использованием javascript, где я устанавливаю две переменные, которые мне нужны для вызова моей функции с этими переменными, следуя моим кодам:

C # — код, лежащий в основе:

  public string CallWebMethod(string url, Dictionary<string, string> dicParameters)
{
    try
    {


        byte[] requestData = this.CreateHttpRequestData(dicParameters);
        HttpWebRequest httpRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        httpRequest.Method = "POST";
        httpRequest.KeepAlive = false;
        httpRequest.ContentType = "application/json; charset=utf-8";
        httpRequest.ContentLength = requestData.Length;
        httpRequest.Timeout = 30000;
        HttpWebResponse httpResponse = null;
        String response = String.Empty;

        httpRequest.GetRequestStream().Write(requestData, 0, requestData.Length);
        httpResponse = (HttpWebResponse)httpRequest.GetResponse();
        Stream baseStream = httpResponse.GetResponseStream();
        StreamReader responseStreamReader = new StreamReader(baseStream);
        response = responseStreamReader.ReadToEnd();
        responseStreamReader.Close();

        return response;
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

private byte[] CreateHttpRequestData(Dictionary<string, string> dic)
{
    StringBuilder sbParameters = new StringBuilder();
    foreach (string param in dic.Keys)
    {
        sbParameters.Append(param);//key => parameter name
        sbParameters.Append('=');
        sbParameters.Append(dic[param]);//key value
        sbParameters.Append('amp;');
    }
    sbParameters.Remove(sbParameters.Length - 1, 1);

    UTF8Encoding encoding = new UTF8Encoding();

    return encoding.GetBytes(sbParameters.ToString());

} 
  

и это мой javascript :

 <script>

function SendNeedHelpLinkTrace() {

    var keysToSend = ['pCatchLinkVirement', 'pCatchLinkCarteBancaire', 'pCatchLinkRechargePaiementFactureTelecom', 'pCatchLinkPaiementVignetteImpotTaxe', 'pCatchLinkPaiementFactureEauElectricite', 'pCatchLinkServiceFatourati', 'pCatchLinkCihExpress', 'pCatchLinkEdocuments']

    var lChannelId = document.getElementById('<%= HiddenChannelId.ClientID%>').value;
    var lServiceId = "900149";
    var lClientId = document.getElementById('<%= HiddenClientId.ClientID%>').value;

    //alert(lClientId);

    var lData = keysToSend.reduce(function(p,c){
        var _t = sessionStorage.getItem(c);
        return isEmpty(_t) ? p : p   ' | '   _t;
    }, '')


    function isEmpty(val){
        return val === undefined || val === null;
    }

    var lCollect;

        console.log(lClientId);

        console.log(lData);

        alert(lData);
         // this is the dictionnary:
        lDataCollected = lClientId   ";"   lChannelId   ";"   lServiceId   ";"   lData;

        console.log(lDataCollected);

        //this is the url:
        var url="http://10.5.230.21:4156/CatchEvent.asmx/CollectData";

         sessionStorage.clear();
      }
  

Как мне поступить?

Комментарии:

1. Проверьте, что такое jquery ajax

2. @mybirthname мне нужно использовать метод, отличный от ajax! есть предложения?