GET方法始终接收空值

时间:2018-10-09 22:58:37

标签: javascript jquery ajax asp.net-mvc

当模态出现时,我试图用Ajax获取列表,但是由于某种原因,我的方法总是收到一个null变量。我确定问题出在Ajax调用上,因为如果我使用1作为参数而不是Ajax的值来测试我的方法,它将以我想要的方式返回列表。

JS:

$("#visualizacao").on('show.bs.modal', function (e) {
            var data = $(e.relatedTarget);
            var idAviso = data.context.dataset.avisoid;
                $.ajax({
                    type: 'GET',
                    url: 'ListaVisuAviso/' +idAviso,
                    success: function (response) {
                        $('#visu-table tbody').empty(); 
                        var trHTML = '';
                        $.each(response, function (i, item) {
                            trHTML += '<tr><td>' + item.NOME + '</td><td>' + item.DATA_HORA + '</td><td>' + item.DEPARTAMENTO + '</td></tr>';
                        });
                        $('#visu-table tbody').append(trHTML);
                        $('#modal-visu').modal('show');
                    },
                    error: function (xhr) {
                        console.log(xhr);
                    }
                });
            $('#modalAviso').modal('show');
        });

C#

    [HttpGet]
    public JsonResult ListaVisuAviso(string avisoId)
    {
        //var avisoid = 1;
        var avisoid = Convert.ToDecimal(avisoId);

        var query =
            from a in _dataContext.TB_AVISOS_NOTIFICACOES
            join b in _dataContext.VW_USUARIOS4 on a.USUARIO_PR equals b.USUARIOID
            where a.AVISO_ID == avisoid
            select new VisuAviso()
            {
                NOME = b.NOME,
                DATA_HORA = a.DATA_HORA.ToString(),
                DEPARTAMENTO = b.DEPARTAMENTO
            };
        return Json(query, JsonRequestBehavior.AllowGet);
    }

2 个答案:

答案 0 :(得分:1)

我发现了造成“问题”的原因。要使用这种发送参数的方式,我后端的路由配置期望它是一个名为“ id”的参数。因此,我可以将接收参数改为“ id”而不是“ avisoId”,如下所示:

AWS.config.update({
                region:'us-east-2',
                credentials: new AWS.CognitoIdentityCredentials({
                    IdentityPoolId: 'identity_pool_id'
                })
            }); 
            let cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
            let userPool = 'user_pool_id';
            var params = {
                UserPoolId: userPool,
                Username: req.body.customer.email
            };
            cognitoidentityserviceprovider.adminGetUser(params, function (err, data) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(data);
                }
            });

或者,我会在JS上指定参数的名称,例如“?usuarioId =“,这样路由将知道它不是id参数:

CredentialsError: Missing credentials in config
     at Request.extractError (/user/node_modules/aws-sdk/lib/protocol/json.js:48:27)
     at Request.callListeners (/user/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
     at Request.emit (/user/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
    at Request.emit (/user/node_modules/aws-sdk/lib/request.js:683:14)
     at Request.transition (/user/node_modules/aws-sdk/lib/request.js:22:10)
     at AcceptorStateMachine.runTo (/user/node_modules/aws-sdk/lib/state_machine.js:14:12)
     at /user/node_modules/aws-sdk/lib/state_machine.js:26:10
     at Request.<anonymous> (/user/node_modules/aws-sdk/lib/request.js:38:9)
     at Request.<anonymous> (/user/node_modules/aws-sdk/lib/request.js:685:12)
   originalError: 
    { message: 'Could not load credentials from CognitoIdentityCredentials',
      code: 'CredentialsError',
      time: 2018-10-10T14:39:04.356Z,
      requestId: '3c381b82-cc9a-11e8-b5fd-6728c4333ce8',
      statusCode: 400,
      retryable: false,
      retryDelay: 95.84437296226204,
      originalError: 
       { message: 'Invalid identity pool configuration. Check assigned IAM roles for this pool.',
         code: 'InvalidIdentityPoolConfigurationException',
         time: 2018-10-10T14:39:04.355Z,
        requestId: '3c381b82-cc9a-11e8-b5fd-6728c4333ce8',
         statusCode: 400,
         retryable: false,
         retryDelay: 95.84437296226204 } }

答案 1 :(得分:0)

尝试将属性 dataType:'json'放入您的Ajax请求中。

     $("#visualizacao").on('show.bs.modal', function (e) {
        var data = $(e.relatedTarget);
        var idAviso = data.context.dataset.avisoid;
            $.ajax({
                type: 'GET',
                url: 'http://localhost/website/ListaVisuAviso/' +idAviso,
                dataType:'json',
                success: function (response) {
                    $('#visu-table tbody').empty(); 
                    var trHTML = '';
                    $.each(response, function (i, item) {
                        trHTML += '<tr><td>' + item.NOME + '</td><td>' + item.DATA_HORA + '</td><td>' + item.DEPARTAMENTO + '</td></tr>';
                    });
                    $('#visu-table tbody').append(trHTML);
                    $('#modal-visu').modal('show');
                },
                error: function (xhr) {
                    console.log(xhr);
                }
            });
        $('#modalAviso').modal('show');
    });

还要验证URL,是否 URL:'ListaVisuAviso /'+ idAviso 指向正确的端点?也许您可以将其替换为用于测试的绝对链接

'http://localhost/website/ListaVisuAviso/' +idAviso