【问题标题】:Error in $Ajax ASP.NET web method call [async]$Ajax ASP.NET Web 方法调用中的错误 [异步]
【发布时间】:2019-04-01 08:29:31
【问题描述】:

我正在处理异步 Web 方法(asmx 文件),我需要在整个 ajax jquery 方法中调用此方法,但是我面临很多问题,因为该方法还使用实体框架来运行其他一些东西。

这里是 JavaScript:

function SubmitStripeForm() {
        // event.preventDefault();
        stripe.createToken(card).then(function (result) {
            if (result.error) {
                // Inform the user if there was an error.
                var errorElement = document.getElementById('card-errors');
                errorElement.textContent = result.error.message;
            } else {
                // Send the token to your server.
                // stripeTokenHandler(result.token);
                console.log();
                var myToken = result.token.id;
                $.ajax({
                    type: "POST",
                    url: "http://localhost:54355/Account/API/Stripe.asmx/MyStripePayment",
                    crossDomain: true,
                    async: false,
                    data: '{Tok: "' + myToken + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        if (response.d) {
                            console.log("Good");
                        }
                        else {
                            console.log("Bad");
                        }
                    },
                    failure: function (response) {
                        console.log("HORRIBLE");
                    }
                });
            }
        });

这是asp.net c#中的web方法:

[WebMethod(EnableSession = true)]
    public async void MyStripePayment(string Tok)
    {   
        string MyToken = Tok;
        using (var context = new CompanyEntities())
        {
            var collection = (from p in context.COMPANY where p.Id == Company_Id select p);
            foreach (var item in collection)
            {
                // Validation of the object
                BillingManagement.Michael Maik = new BillingManagement.Michael();
                Maik.name = "Michael";
                Maik.id = "114060502";
                Maik.number = "83290910";


                #region Send Information To Stripe
                CancellationToken Token = new CancellationToken();
                string Url = "http://localhost:5000/testing/give-your-data";
                using (var client = new HttpClient())
                using (var request = new HttpRequestMessage(HttpMethod.Post, Url))
                {
                    var json = JsonConvert.SerializeObject(Maik);
                    using (var stringContent = new StringContent(json, Encoding.UTF8, "application/json"))
                    {
                        request.Content = stringContent;

                        using (var response = await client
                            .SendAsync(request, HttpCompletionOption.ResponseHeadersRead, Token)
                            .ConfigureAwait(false))
                        {
                            response.EnsureSuccessStatusCode();
                            string resString = await response.Content.ReadAsStringAsync();
                            JObject jObject = JObject.Parse(resString);
                            string message = (string)jObject["message"];
                        }
                    }
                }
                #endregion

            }
        }

这是显示的错误:

POST http://localhost:54355/Account/API/Stripe.asmx/MyStripePayment 500 (Internal Server Error)

【问题讨论】:

    标签: c# jquery ajax web-services asynchronous


    【解决方案1】:

    您是否尝试过使用Try/Catch 来获取有关错误的更多信息?显然这是内部问题,最好这样使用它,然后再试一次看看它是什么错误。

    try
    {
        // Code
    }
    catch (Exception ex)
    {
       console.writeLine(ex);         
    }
    

    您在 catch 中进行干预,以便能够在局部变量中看到“Ex”的值,或者将错误保存在某处以供以后读取。

    【讨论】:

      猜你喜欢
      • 2011-11-01
      • 2015-04-10
      • 2015-06-04
      • 2017-11-23
      • 2011-08-28
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多