【问题标题】:Can't Get Content - Give Me Ajax Error (js)无法获取内容 - 给我 Ajax 错误 (js)
【发布时间】:2019-08-14 07:27:11
【问题描述】:

这就是我尝试通过 json 向我的 javascript 发送内容的方式。来自 Stripe 向 3d (SCA) 付款

问题出在来自 strip API 的值上,它告诉我它是“值不能为空。参数名称:输入”,当我只运行它时它就会出现用js(ajax)

[HttpPost]
    [Produces("application/json")]
    [Route("Members/AddMembership/{id}/{CompaniesId}")]
    public async Task<IActionResult> AddMembership(MembersView model)
    {
        try
        {
            //more here....
            model.PiinVoice = subscription.LatestInvoice.PaymentIntent.ClientSecret;
            //more here....

            return Json(model.PiinVoice);
        }
        catch(Exception e)
        {
            TempData[TempDataClass.Error] = true;
            TempData[TempDataClass.ErrorMsg] = HelperText.ExceptionError + e.Message;
            //return RedirectToAction("", "User");

            return Json(e.Message);
        }
    }

Javascript:

$.ajax({
        url: '/Members/AddMembership/' + Id + '/' + CompaniesId, //jump to httppost action,meet [Route("Members/AddMembership/{id}/{CompaniesId}")]
        dataType: "json",
        data: "",
        type: "Post",
        contentType: 'application/json',
        success: function (result) {//receive response from httppost action
            //result is the response of the httppost action,you can handle it
            console.log("New: " + Id, CompaniesId);
            console.log(result.PiinVoice)
            //var paymentIntentSecret = document.getElementById('PiinVoice').value;
            //stripe.handleCardPayment(paymentIntentSecret).then(function (result) {
            //    if (result.error) {
            //        // Display error.message in your UI.
            //        // Inform the user if there was an error.
            //        var errorElement = document.getElementById('card-errors');
            //        errorElement.textContent = result.error.message;
            //    } else {
            //        // The payment has succeeded. Display a success message.
            //        stripeTokenHandler(result.token);
            //    }
            //});
        },
        error: function (xhr) {
            console.log(xhr)
        }
    });

如果我查看 result.PiinVoice 那么它给了我 undefined 但如果我只写结果然后它给我“值不能为空。参数名称: 输入"

所以我想对我的问题做的是:我怎样才能完成,以便我通过 json 发送的结果进入 js,然后我可以使用它来剥离。

由于现在的问题是我的控制器中的值绝不会出现在我的 js ajax 中

【问题讨论】:

  • 尝试调试代码,看看是否在 pinvoice 中获取数据
  • 我已经尝试调试:imgur.com/EZP34K4 就像我看不到哪里出了问题。或许你能明白为什么我不能调试js和控制器了。

标签: javascript c# json


【解决方案1】:

首先在您的 ajax 代码中将代码 console.log(result.PiinVoice) 更改为 console.log(result)。您会注意到您将PiinVoice 发送为result

将您的控制器更改为返回 Json(model); 而不是返回 Json(model.PiinVoice);

【讨论】:

  • 好的,我将修复我的 consol.log 仅用于结果,同时我的 json 仅用于模型。现在是这样的。 model.PiinVoice = subscription.LatestInvoice.PaymentIntent.ClientSecret; return Json(model);console.log(result)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2022-11-09
  • 2022-01-13
  • 1970-01-01
  • 2022-06-11
  • 2014-10-01
  • 1970-01-01
相关资源
最近更新 更多