简体   繁体   English

如何从脚本页面返回json数据?

[英]How to return back json data from script page?

I had a json file results.json Which shown below. 我有一个json文件results.json,如下所示。 And I had a html file contain some script. 我有一个包含一些脚本的html文件。 This is for retrieve data data. 这用于检索数据数据。 When I am enter into the html page which call a script function get_machFollow(que_script) this function is for receive json file data. 当我进入调用脚本函数get_machFollow(que_script)的html页面时,此函数用于接收json文件数据。 The function is works fine and which alert correct output, But after this function return some data to my HTML page. 该函数工作正常,并提醒正确的输出,但在此函数后返回一些数据到我的HTML页面。

My JSON file 我的JSON文件

{"mach_fol_4": {"match_l":
       ["7","8","99"],"attempts":"0","feedback_true":"You are right!",
  "feedback_false":"Sorry! wrong answer."}}

This is my script function. 这是我的脚本功能。 This function is works fine but I can't alert the return value from HTML page. 此功能工作正常,但我无法从HTML页面提醒返回值。 That shows undefined. 这显示未定义。

function get_machFollow(que_script)
{

        var return_var;
      $.getJSON('results.json', function(data) {
            return_var=data[que_script].match_r;    
            alert(return_var);//Working alert show correct output 
            return return_var;     
                 });

}

This is my html file 这是我的html文件

   <html>
    <head>
      <script type='text/javascript' src='js/jquery.min.js'></script>
      <script>
         $(document).ready(function(){
             var mach_follow_js;
             mach_follow_js=get_machFollow('mach_fol_4');
             alert(mach_follow_js);//Wrong output
         });
   </head>
    <body>
      <p>Hello world</p>
    </body>
    </html>

are you intending return return_var; 你打算return return_var; to be inside the get_machFollow scope, because right now its inside the jquery function scope and will not return value to the main page 在get_machFollow范围内,因为它现在在jquery函数范围内并且不会将值返回到主页面

There are multiple ways by which you can do it. 有多种方法可以做到这一点。 One of them is pass a callback handler to your method which will be called when you get the response. 其中一个是将一个回调处理程序传递给您的方法,当您获得响应时将调用该方法。 Try this: 尝试这个:

function get_machFollow(que_script, sCallback)
{
      var return_var;
      $.getJSON('results.json', function(data) {
            return_var=data[que_script].match_r;    
            alert(return_var);//Working alert show correct output 
            sCallback.call(null /* context */, return_var);      
       });

}

$(document).ready(function(){
    var mach_follow_js;
    get_machFollow('mach_fol_4', function(output) {
        alert(output);
        match_follow_js = output;
   });
});

Here below JSON data fetched by AJAX. 下面是AJAX提取的JSON数据。 It passing JSON Data Object in Alert. 它在Alert中传递JSON数据对象。 You can use it as you want. 您可以根据需要使用它。 also can Iterate data using for loop or $.each function. 也可以使用for循环或$ .each函数迭代数据。

$(document).ready(function(){
    var mach_follow_js;
    // mach_follow_js=get_machFollow('mach_fol_4');

    //JSON Data Fetched by AJAX
    $.ajax('results.json',{
        type:'GET',
        dataType: "json",
        jsonCallback: 'successCallback',               
        async: true,
        beforeSend: function () {
        //if you want to show loader
    },
    complete: function () {
        //hide loader after download data
    },
    success: function (resultJSON) {                                       
        mach_follow_js = resultJSON;        // assigning to Global variable ProductResult                   
        alert(mach_follow_js);
        console.log(mach_follow_js);
    },
    error: function (request, error) {
        alert('Network error has occurred please try again!');//error
    }
    })                              

});

Use ajax callback function $.getJSON() is actually an ajax function. 使用ajax回调函数$.getJSON()实际上是一个ajax函数。 So you need to apply callback to perform this action. 因此,您需要应用回调来执行此操作。

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

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