Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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
Javascript 页面加载时的.get()请求_Javascript_Jquery_Html - Fatal编程技术网

Javascript 页面加载时的.get()请求

Javascript 页面加载时的.get()请求,javascript,jquery,html,Javascript,Jquery,Html,我将把用户重定向到一个页面。此页面从JSON格式的RESTAPI获取其所有内容。因此,在页面加载时,我希望执行$.get()请求并在页面中加载我的div的内容 我知道如何执行get请求,但是,我不知道如何在页面加载时执行。我的整个应用程序都有一个application.js文件。因此,我无法将其放入document.ready中,因为它会加载到应用程序的每个页面上 我将执行如下get请求: $.get( $(this).data('myurl'), function (data) {

我将把用户重定向到一个页面。此页面从JSON格式的RESTAPI获取其所有内容。因此,在页面加载时,我希望执行
$.get()
请求并在页面中加载我的div的内容

我知道如何执行
get
请求,但是,我不知道如何在页面加载时执行。我的整个应用程序都有一个
application.js
文件。因此,我无法将其放入
document.ready
中,因为它会加载到应用程序的每个页面上

我将执行如下
get
请求:

$.get(
  $(this).data('myurl'),
  function (data) {
    var item = data.response.item[0];
    $('mydiv').html(item.text);
  }
);

使用jQuery很容易,但您必须将此块放在“开始”页面中:

$(function() {
  // this code will be executed on page load
  app.start();
});

使用jQuery很容易,但您必须将此块放在“开始”页面中:

$(function() {
  // this code will be executed on page load
  app.start();
});

将其添加为带有单独document.ready的内联脚本块,或在
之前添加


或者将其添加到单独的js文件中,并仅从要使用它的特定页面链接它。

将其添加为带有单独document.ready的内联脚本块,或者在
之前添加


或者将其添加到单独的js文件中,并仅从您想要使用它的特定页面链接它。

据我所知,您只想从特定页面上的rest api获取数据

为此,您可以:

$(function(){
    if (window.location.href.indexOf('/your/page/url') > -1){
        // your $.get here
        $.get('/torestapi', function(){ //update divs });
    }
});

据我所知,您只想从特定页面上的RESTAPI获取数据

为此,您可以:

$(function(){
    if (window.location.href.indexOf('/your/page/url') > -1){
        // your $.get here
        $.get('/torestapi', function(){ //update divs });
    }
});

然后重命名页面并搜索小时数为什么不再工作:)然后重命名页面并搜索小时数为什么不再工作:)