$ Ajax ASP.NET Web方法调用[async]

时间:2018-10-28 03:01:34

标签: c# jquery ajax web-services asynchronous

我正在处理异步Web方法(asmx文件),并且需要在ajax jquery方法中调用此方法,但是由于该方法还使用Entity Framework来运行其他内容,因此我遇到了很多问题。

这是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)

1 个答案:

答案 0 :(得分:0)

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

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

OR

您对捕获进行了干预,以便能够在局部变量中看到“ Ex”的值,或者将错误保存在某个地方以便以后读取。