Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将ajax响应保存到jquery变量_Jquery_Ajax - Fatal编程技术网

将ajax响应保存到jquery变量

将ajax响应保存到jquery变量,jquery,ajax,Jquery,Ajax,简单的问题,, 我知道ajax是异步的,我有下面的函数,它与响应警报一起工作,但我的问题是,我想知道如何将ajax响应的结果存储在模式变量中,以及在按下按钮显示模式时显示的其他文本中。。我尝试过async:false,但它不起作用..:(这是我的职责 getVersion: function() { $.ajax({ dataType: 'json', type: 'GET', url: this.options.url.generalsU

简单的问题,, 我知道ajax是异步的,我有下面的函数,它与响应警报一起工作,但我的问题是,我想知道如何将ajax响应的结果存储在模式变量中,以及在按下按钮显示模式时显示的其他文本中。。我尝试过async:false,但它不起作用..:(这是我的职责

     getVersion: function() {
     $.ajax({
      dataType: 'json',
      type: 'GET',
      url: this.options.url.generalsUrl + '/getAppVersion',
      cache: false,
      async: false,
      success: function(response) {
      alert(response.VERSION);//ok this is for testing
      },
      error: function(jqXHR, exception) {
          alert("There was an error retrieving the version of the application.");
      }
  });
},

但是在模态按钮功能的其他地方,我想把它放在var内容的其他文本中,这里是另一个没有显示所有内容的函数,因为我不想懒惰

    var imgAbout = $('#topright-menu ul.menu-divmenu > li > a > img[src$="about.png"]');
    imgAbout.click(function(e) {
        e.preventDefault();
        var modal = (self.modal());      
var content = $('self.getVersion()') + //this line is bad how can i show the response?
              "in the following browsers have been tested: <br>" +
              '<img src="images/ie.png" width="45" height="45"/>Internet Explorer 11.0.9600.17239<br>' +
              '<img src="images/mozilla.png" width="45" height="45"/>Mozilla Firefox 32.0<br>' + 
              '<img src="images/chrome.png" width="45" height="45"/>Google Chrome 37.0.2062.103 m<br>';

        var $div_container = $('<div></div>')
                .addClass('jtable-main-container');

        var $div_title = $('<div></div>').addClass('jtable-title')
                .appendTo($div_container);
        $('<div></div>').addClass('jtable-title-text')
                .text('Information for the application')
                .appendTo($div_title);

        var $table = $('<table></table>').appendTo($div_container);
        var $row = $('<tr></tr>').appendTo($table);
        var $cell = $('<td></td>').html(content).appendTo($row);                   
        $('#jsn-pos-left').hide();
        modal.open({
            content: $div_container                
        });

    });
var-imgAbout=$(右上角菜单ul.menu-divmenu>li>a>img[src$=“about.png]”);
imgaout.单击(函数(e){
e、 预防默认值();
var modal=(self.modal());
var content=$('self.getVersion()')+//这一行不好,如何显示响应?
“已在以下浏览器中测试:
”+ “Internet Explorer 11.0.9600.17239
”+ “Mozilla Firefox 32.0
”+ “谷歌浏览器37.0.2062.103 m
”; 变量$div_容器=$(“”) .addClass(“jtable-main-container”); var$div_title=$('').addClass('jtable-title')) .appendTo($div_container); $('').addClass('jtable-title-text') .text('应用程序信息') .附录($div_title); var$table=$('').appendTo($div_容器); var$row=$('').appendTo($table); var$cell=$('').html(内容).appendTo($row); $('jsn pos left').hide(); 模态开放({ 内容:$div_容器 }); });

有人帮忙吗?我相信这是件容易的事,但对我来说很难:(

你应该使用jQuery来完成这项工作

 getVersion: function() {
   var d = new $.Deferred();
   $.ajax({
     dataType: 'json',
     type: 'GET',
     url: this.options.url.generalsUrl + '/getAppVersion',
     cache: false,
     async: false,
     success: function(response) {
        d.resolve(response.VERSION);
        alert(response.VERSION);//ok this is for testing
     },
     error: function(jqXHR, exception) {
         alert("There was an error retrieving the version of the application.");
     }
   });
   return d.promise();
 }. ...

然后您可以简单地调用var version=yourObj.getVersion();

您有三个简单的选择

  • 当页面准备就绪时加载版本-如果此版本没有更改,这很好。然后,您可以简单地将其保存在全局变量中,并在为您的模板构建
    内容
    html字符串时使用它

  • 如果您希望在模式打开时加载版本,那么您可以在单击事件中请求AJAX,并在AJAX回复处理程序(
    success
    function)中构建
    content
    html字符串并在那里显示模式。如果AJAX回复足够快,这很好

  • 如果AJAX回复很慢,并且您希望在单击时立即显示模式,那么您可以在内容字符串中放入占位符,并用DOM操作替换,例如

    // in the click handler show the modal:
    var content =  "<span class='version'> in the following browsers have..."
    // show modal
    // then call ajax, and in success function fill the placeholder:
    $('.version').text('Version is '+ajax_result)
    
    //在单击处理程序中显示模式:
    var content=“在以下浏览器中有…”
    //显示模态
    //然后调用ajax,并在成功函数中填充占位符:
    $('.version').text('版本为'+ajax\u结果)
    

  • (您可能希望放置一个旋转的“加载”图标作为占位符)

    在对话框中为ajax结果创建占位符。并在加载时将ajax请求的结果放置到此占位符


    …首先非常感谢你!!! Iftah 张 Azadrum 谢谢你的帮助!!! 因此,只是为了将来在其他人需要它时使用..我更改了它,并在getVersion函数中加入了模态函数,它工作起来很有魅力,下面是工作代码。。 按钮

          var imgAbout = $('#topright-menu ul.menu-divmenu > li > a > img[src$="about.png"]');
          imgAbout.click(function(e) {
            e.preventDefault();            
            self.getVersion();      
      });
    
    对于包含模态函数的getVersion函数

          getVersion: function() {      
        var self = this;      
        $.ajax({
            dataType: 'json',
            type: 'GET',
            url: this.options.url.generalsUrl + '/getAppVersion',
            cache: false,
            async: false,
            success: function(response) {            
                var modal = (self.modal());      
                var content = "<span class='version'>" + "The Version of the application is: " + response.VERSION + "<br>" +
                "The application has been tested with the following browsers: " + "<br>" +
                '<img src="../images/browser_icons/ie.png" width="45" height="45"/>Internet Explorer 11.0.9600.17239<br>' +
                '<img src="../images/browser_icons/mozilla.png" width="45" height="45"/>Mozilla Firefox 32.0<br>' + 
                '<img src="../images/browser_icons/chrome.png" width="45" height="45"/>Google Chrome 37.0.2062.103 m<br>';
                var $div_container = $('<div></div>')
                .addClass('jtable-main-container');                                
                var $div_title = $('<div></div>').addClass('jtable-title')
                .appendTo($div_container);
                $('<div></div>').addClass('jtable-title-text')
                .text('Πληροφορίες για την εφαρμογή')
                .appendTo($div_title);
                var $table = $('<table></table>').appendTo($div_container);
                var $row = $('<tr></tr>').appendTo($table);
                var $cell = $('<td></td>').html(content).appendTo($row);                   
                $('#jsn-pos-left').hide();
                modal.open({
                    content: $div_container                
                });
            },
            error: function(jqXHR, exception) {
                alert("There was an error retrieving the version of the application.");
            }
        });
    },      
    
    getVersion:function(){
    var self=这个;
    $.ajax({
    数据类型:“json”,
    键入:“GET”,
    url:this.options.url.generalsUrl+'/getAppVersion',
    cache:false,
    async:false,
    成功:功能(响应){
    var modal=(self.modal());
    var content=“+”应用程序的版本为:“+response.Version+”
    ”+ 已使用以下浏览器对应用程序进行了测试:“+”
    ”+ “Internet Explorer 11.0.9600.17239
    ”+ “Mozilla Firefox 32.0
    ”+ “谷歌浏览器37.0.2062.103 m
    ”; 变量$div_容器=$(“”) .addClass(“jtable-main-container”); var$div_title=$('').addClass('jtable-title')) .appendTo($div_container); $('').addClass('jtable-title-text') .文本(‘πληρορίεςγιατηνεφαρμογή’) .附录($div_title); var$table=$('').appendTo($div_容器); var$row=$('').appendTo($table); var$cell=$('').html(内容).appendTo($row); $('jsn pos left').hide(); 模态开放({ 内容:$div_容器 }); }, 错误:函数(jqXHR,异常){ 警报(“检索应用程序版本时出错。”); } }); },

    再次感谢大家的帮助..问题解决了!主题结束。

    非常感谢您-不客气!但在本网站中,您可以向上投票(向左上箭头)并接受其中一个答案(选中左边的标记),而不是说“谢谢”。这意味着“谢谢”,也会奖励回答者,也会将问题从“未回答”列表中删除,而且最重要的是帮助未来的读者找到好的答案。
            modal.open({
                content: $div_container                
            });
    
    self.getVersion();
    
          var imgAbout = $('#topright-menu ul.menu-divmenu > li > a > img[src$="about.png"]');
          imgAbout.click(function(e) {
            e.preventDefault();            
            self.getVersion();      
      });
    
          getVersion: function() {      
        var self = this;      
        $.ajax({
            dataType: 'json',
            type: 'GET',
            url: this.options.url.generalsUrl + '/getAppVersion',
            cache: false,
            async: false,
            success: function(response) {            
                var modal = (self.modal());      
                var content = "<span class='version'>" + "The Version of the application is: " + response.VERSION + "<br>" +
                "The application has been tested with the following browsers: " + "<br>" +
                '<img src="../images/browser_icons/ie.png" width="45" height="45"/>Internet Explorer 11.0.9600.17239<br>' +
                '<img src="../images/browser_icons/mozilla.png" width="45" height="45"/>Mozilla Firefox 32.0<br>' + 
                '<img src="../images/browser_icons/chrome.png" width="45" height="45"/>Google Chrome 37.0.2062.103 m<br>';
                var $div_container = $('<div></div>')
                .addClass('jtable-main-container');                                
                var $div_title = $('<div></div>').addClass('jtable-title')
                .appendTo($div_container);
                $('<div></div>').addClass('jtable-title-text')
                .text('Πληροφορίες για την εφαρμογή')
                .appendTo($div_title);
                var $table = $('<table></table>').appendTo($div_container);
                var $row = $('<tr></tr>').appendTo($table);
                var $cell = $('<td></td>').html(content).appendTo($row);                   
                $('#jsn-pos-left').hide();
                modal.open({
                    content: $div_container                
                });
            },
            error: function(jqXHR, exception) {
                alert("There was an error retrieving the version of the application.");
            }
        });
    },