无法获取内容-给我Ajax错误(js)

时间:2019-08-14 07:27:11

标签: javascript c# json

这是我尝试通过json将某些内容发送到我的javascript的方法。来自Stripe,要付款给3d(SCA)

问题出在带状API的值中,告诉我它是“ 值不能为空。参数名称:input ”,当我只运行它时就会出现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 ,那么它会给我未定义,但是如果我只写结果,那么它会给我“ 值不能为空。参数名称” :输入

所以我想对我的问题做的是:我该如何做,以便我通过json发送的结果进入js,然后我可以用来剥离。

现在的问题是,控制器中的值绝不会传递给我的js ajax

1 个答案:

答案 0 :(得分:0)

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

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