简体   繁体   English

无法通过Jquery(Ajax)调用发送对象

[英]Unable to send object from Jquery(Ajax) call

I am trying to send data to MongoLab DB using JS+ JqueryAjax call. 我正在尝试使用JS + JqueryAjax调用将数据发送到MongoLab数据库。 My problem is i am able to see the OID in DB but no Data is populated. 我的问题是我能够在数据库中看到OID,但没有填充任何数据。 When checked in code , Network settings my payload is always empty. 在签入代码后,网络设置我的有效载荷始终为空。 I am unable to figure out why my object is misbehaving. 我无法弄清楚为什么我的对象行为异常。

    <!DOCTYPE html>
<html lang="en">
<head>
    <title>Boom!</title>
    <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script>
    </head>
<body>
    <div class="container">
        <h1>Stuff.</h1>
        <p>
            <button class="btn btn-large">Press Me!</button>

        </p>
        <form name="ajaxform" id="ajaxform"  method="POST">
First Name: <input type="text" name="fname" id="fname" value =""/> <br/>
Last Name: <input type="text" name="lname"  id="lname" value ="" /> <br/>
</form>

    </div>

</body>
</html>
    <script>
        var stuff = {
            run: function () {
                $('button').bind('click', function () {
                    if ('geolocation' in navigator) {
                        var geo_options = {
                            name: fname.value ,
                            surname: lname.value

                        };
                        navigator.geolocation.getCurrentPosition(stuff.send, stuff.error, geo_options);
              window.alert(geo_options.fname);
              document.write(geo_options.fname.value);
              console.log(geo_options.fname.value);

                    }
                });
            },
            error: function (msg) {
                console.log(msg);
            },
            send: function (object) {

                $.ajax({
                    url: "https://api.mongolab.com/api/1/databases/geolocation/collections/boom?apiKey=[apiKey]",
                    type: "POST",
                    data: JSON.stringify( object ),
                    contentType: "application/json; charset=utf-8"
                }).done(function( msg ) {
                    console.log(msg);
                });
            }
        };
        $(function() {
            stuff.run();
        });
    </script>

I always see my object as undefines in popups and console. 我总是在弹出窗口和控制台中将对象视为未定义对象。 I have just started using JS and Jquery. 我刚刚开始使用JS和Jquery。

fname and lname are not defined. 未定义fnamelname To access their values, you need to use these two functions: $("#fname").val() and $("#lname").val() . 要访问它们的值,需要使用以下两个函数: $("#fname").val()$("#lname").val() I'm surprised you're getting undefined in your console instead of an error. 我很惊讶您在控制台中得到的是未定义状态而不是错误。

And when you try to access those value in geo_options , you aren't using the right keys. 而且,当您尝试访问geo_options值时,您没有使用正确的键。 window.alert(geo_options.fname); should be window.alert(geo_options.name); 应该是window.alert(geo_options.name);

I would also keep the run, error and send functions outside of the object stuff , as there seems to be no need for it. 我也将保持运行,错误和发送的对象之外的功能stuff ,因为似乎没有必要了。 And I think those functions may need to be inside of the document ready snippet (what you have at the bottom) for the selectors to be guaranteed to work, but I'm not sure. 而且我认为这些功能可能需要放在文档准备就绪的代码片段(位于底部)中,以确保选择器可以正常工作,但我不确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM