[英]jsFiddle jQuery ajax function not working onLoad
I have an jQuery ajax function within jsFiddle 我在jsFiddle中有一个jQuery ajax函数
Is this possible or am I missing something? 这可能吗,或者我缺少什么?
Here is the jsFiddle: 这是jsFiddle:
http://jsfiddle.net/djlerman/bbj8k9pe/ http://jsfiddle.net/djlerman/bbj8k9pe/
I tried the delay option but that didn't fix the issue. 我尝试了延迟选项,但没有解决问题。
// Run ajax function onLoad -- DOESN'T WORK getNodeViaAjax(); // Run ajax function ON Button click -- WORKS $( "#buttonID" ).on( "click", "", function() { getNodeViaAjax(); }); // ajax function function getNodeViaAjax() { $.ajax({ type: 'POST', url: '/echo/json/', data: { json: JSON.stringify( jsonData ) }, success: function(data) { $('#displayResponse').html(JSON.stringify(data)); }, error:function(error){ alert('there was an error'); }, dataType: 'json' }); } // Data to return via ajax /* This is an echo of some data sent back via ajax */ /* This data should be filtered by nodeID and return only childNodeID's. */ /* vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv. */ var jsonData = { "nodeID": { "1": { "childNodeID": { "1.1": { "childNodeType": "branch", "childData": [ "1.1: column 1", "1.1: column 2" ] }, "1.2": { "childNodeType": "leaf", "childData": [ "1.2: column 1", "1.2: column 2" ] }, "1.3": { "childNodeType": "leaf", "childData": [ "1.3: column 1", "1.3: column 2" ] } } }, "1.1": { "childNodeID": { "1.1.1": { "childNodeType": "leaf", "childData": [ "1.1.1: column 1", "1.1.1: column 2" ] }, "1.1.2": { "childNodeType": "leaf", "childData": [ "1.1.2: column 1", "1.1.2: column 2" ] } } }, "2": { "childNodeID": { "2.1": { "childNodeType": "leaf", "childData": [ "2.1: column 1", "2.1: column 2" ] }, "2.2": { "childNodeType": "leaf", "childData": [ "2.2: column 1", "2.2: column 2" ] }, "2.3": { "childNodeType": "leaf", "childData": [ "2.3: column 1", "2.3: column 2" ] } } } } }; /* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ */ /* This is an echo of some data sent back via ajax */ /* This data should be filtered by nodeID and return only childNodeID's. */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <button id="buttonID">Click to Load ajax</button> <div id="displayResponse"></div>
If you add a quick console.log
statement, you'll find that your jsonData
is undefined... 如果添加快速
console.log
语句,则会发现jsonData
未定义...
function getNodeViaAjax() {
console.log('Running now...' + jsonData);
You are attempting to send that data in the POST, so make sure the data is initialized before you use it... although the variable is hoisted, the place where the value is set is not - so move your var jsonData = ...
to before your getNodeViaAjax()
function call. 您正尝试在POST中发送该数据,因此请确保在使用该数据之前已对其进行初始化...尽管已悬挂该变量,但未设置该值的位置-因此请移动
var jsonData = ...
到getNodeViaAjax()
函数调用之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.