AJAX JQuery的JSON数据后未达到控制器 [英] AJAX JQuery JSON Post Data not Reaching Controller

查看:196
本文介绍了AJAX JQuery的JSON数据后未达到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

var url =  base_url+"ajax/user/list_user_info/"+userId;
$.ajax({
        type: "POST",
        url: url,
        data:{user_id:userId},
        contentType: 'application/json',
        dataType: 'JSON',
        success: function(data) {

            if(data.redirect){
                window.location = data.redirect;    
            }
            else {
                $('#profile-main-content').html(data.html);
            }    
        }      
    });

现在,这是我的请求,pretty的标准的AJAX请求。我的问题是,该请求未到达控制器的某些原因。要检查我已经进行了简单的测试插入在数据库中查询的结果为控制器的功能的第一道防线。但测试没有得到插入。网址是正确的,因为我检查了BASE_URL的声明。我知道,我把用户id作为参数,并通过它作为POST数据,但这是我只是想拼命。任何想法吗?让我知道,如果我不明确什么。谢谢你。

Now, this is my request, a pretty standard AJAX request. My problem is that this request is not reaching the controller for some reason. To check I have performed a simple test insert in the database to check the result at first line of the controller's function. But the test does not get inserted. The url is correct because i checked the declaration of the base_url. I know I put the "userId" as an argument and passed it as POST data, but this is me just trying desperately. Any Ideas please? Let me know if i was not clear about anything. Thanks.

推荐答案

相反的:

data:{user_id:userId}

尝试:

data: JSON.stringify({user_id:userId})

这样做的原因是,您所指定应用程序/ JSON的请求的内容类型,所以你需要发送的JSON。在您的code你是不是发送JSON请求 - 要发送一个简单的应用程序/ x-WWW的形式urlen codeD POST请求。既然你已经通过用户ID在POST身体,你也许并不需要在URL重复。使用一个或其他的方法来发送此信息到你的服务器。

The reason for this is that you specified a request content type of application/json so you need to send JSON. In your code you are not sending a JSON request - you are sending a simple application/x-www-form-urlencoded POST request. And since you have already passed the userId in the POST body you probably don't need to repeat it in your url. Use either one or the other method to send this information to your server.

另外,为了更方便地调试JavaScript错误,我强烈建议您使用萤火。其中很多东西它可以让你看到一个AJAX调用过程中被发送的确切的请求/响应和分析你是从服务器获取确切的错误信息。它使调试问题更​​加容易。

Also in order to more easily debug javascript errors I strongly suggest you to use FireBug. Among many things it allows you to see the exact requests/responses being sent during an AJAX call and analyze the exact error message you are getting from the server. It makes debugging problems much easier.

这篇关于AJAX JQuery的JSON数据后未达到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆