2013-10-25 115 views
7

我正在爲一個網站的證券交易所jQuery修復工作。jQuery AJAX調用返回[對象對象]

編輯:它根據返回的值更新網頁上的ID/CLASS或輸入值。

的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> 

電流LY它返回:

[object Object] 

我也嘗試過用的.text()返回一個div

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 
    } 
] } 

請能有人告訴我,我做錯了什麼。

在此先感謝!

+1

嘗試使用'的console.log(RESP)',這是什麼給。 – MisterBla

+0

向我們顯示回覆,然後我們可以爲您提供幫助。 – mguimard

+0

我們可以看到你的'demo.json'頁面的源代碼嗎?你有沒有嘗試過'console.log(resp);'? –

回答

16

你可以這樣做:

// 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); 
}); 

因此,這裏的主要問題是: -

  • 你需要做的所有result邏輯在阿賈克斯成功回調,如Ajax是asynchronous,你總是得到空值的end & newend變量。
  • 沒有必要做這個result = $(end * newend);因爲它創造了一個新的jQuery對象實例,因此你越來越[object Object]
+1

感謝大家的意見和幫助! @palash 我已經應用這個到我的文件,我得到 遺漏的類型錯誤:無法執行console.log(resp.d.currency [0] .amount)讀的不確定 財產「貨幣」; –

+0

@VaughanThomas好吧,那只是爲了測試/調試的目的,你可以刪除所有'console.log'代碼.. –

9

[對象的對象]基本上是一個數組

嘗試此代碼:

success: function(resp) { 
     //$('#div').val(resp.currency[0].amount); 
     alert(JSON.stringify(resp)); 
    }, 

應該顯示您的陣列,這將給你更好地選擇的元素,以輸出

的能力
+1

這工作對我來說理查 –

1

此代碼

var end = parseInt($('#value').val()); 
var newend = parseInt($('#div').val()); 
var result = $(end * newend); 

被Ajax調用成功之前評估。它需要被移動到成功的塊

1

你計算在產品內部$()

var end = parseInt($('#value').val()); 
var newend = parseInt($('#div').val()); 
var result = end * newend; // this $(end * newend) is probably what was wrong