简体   繁体   English

jsFiddle jQuery ajax函数在onLoad上不起作用

[英]jsFiddle jQuery ajax function not working onLoad

I have an jQuery ajax function within jsFiddle 我在jsFiddle中有一个jQuery ajax函数

  • It works within an onlick 它在一个onlick内工作
  • It DOES NOT work when the page initially loads. 最初加载页面时,它不起作用。

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.

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