jQuery的AJAX调用返回[对象的对象] [英] jQuery AJAX call returns [object Object]

查看:174
本文介绍了jQuery的AJAX调用返回[对象的对象]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个网站证券交易所jQuery的修复。

I am working on a Stock Exchange jQuery fix for a website.

编辑:它会更新根据返回的值网页中的ID / CLASS或输入值

It updates a ID/CLASS or input value on the webpage depending on the value returned.

的index.php:

index.php:

<!doctype html>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<meta charset="utf-8">
<title>load demo</title>
<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
        url: '/datacall/demo.json',
        dataType: 'json',
        success: function( resp ) {
            $( '#div' ).val( resp.currency[0].amount );
        },
        error: function( req, status, err ) {
            console.log( 'Something went wrong', status, err );
        }
    });
    var end = parseInt($('#value').val());
    var newend = parseInt($('#div').val());
    var result = $( end * newend );
    $('#clickme').click(function() {
        $('#new').val( result );
    });
});
</script>

<div>

    <input type="hidden" value="2500" id="value" />

    <input type="hidden" value="" id="div">

    <input type="text" id="new" value="" readonly/>

    <input type="button" value="Change" id="clickme" />

</div> 

目前它返回:

[object Object]

我也尝试过用的.text返回一个div()

I have also tried returning it to a div with .text()

demo.json:

demo.json:

    { "currency" : [
  {
    "name" : "South Africa",
    "code" : "ZAR",
    "amount" : 0.14
  },
  {
    "name" : "America",
    "code" : "USD",
    "amount" : 0.64
  },
  {
    "name" : "Europe",
    "code" : "GBP",
    "amount" : 1.29
  }
] }

请能有人告诉我,我做错了什么。

Please can someone tell me what I did wrong.

在此先感谢!

推荐答案

你可以这样做:

// Create some global variables
var end = parseInt($('#value').val(), 10);
var newend = 0;
var result = 0;

$.ajax({
    url: '/datacall/demo.json',
    dataType: 'json',
    success: function (resp) {

        // Check the values in console
        console.log(resp.currency[0].amount);
        console.log(resp.d.currency[0].amount);

        $('#div').val(resp.currency[0].amount);
        newend = parseInt(resp.currency[0].amount, 10);
        result = end * newend;

        // No need to create a new jQuery object using $()
        // result = $( end * newend );
    },
    error: function (req, status, err) {
        console.log('Something went wrong', status, err);
    }
});

$('#clickme').click(function () {
    $('#new').val(result);
});

因此​​,这里的主要问题是: -

So the main issues here is:-

  • 您需要做的所有的结果逻辑在阿贾克斯成功的回调,因为AJAX是同步而你总是得到空值的结束&放大器; newend 变量。
  • 没有必要做这个的结果= $(结束* newend); ,因为它创建了一个新的jQuery对象实例,因此您收到 [目标对象]
  • You need to do all the result logic in the ajax success callback, as ajax is asynchronous and you always get the empty values for the end & newend variables.
  • No need to do this result = $( end * newend ); as it creates a new jQuery object instance and hence you are getting [object Object]

这篇关于jQuery的AJAX调用返回[对象的对象]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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