te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How can I run ajax code without web server? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I run ajax code without web server? - Stack Overflow

programmeradmin3浏览0评论

I am running ajax without a server on my system, I have created one index.html with this.

JavaScript function:

function do_the_click(url)
{
alert('inside this method do_the_click');
    $.ajax({
                         async: false,
                         url : url, 
                         contentType: "text/html",
                         type : "GET",
                         dataType : "text/html",                
                         success: function(data){
                         alert('data');
           }});
} 

My HTML body content is:

<a href="Java Collections.html" class="button" id="javacollections" onclick="do_the_click('this.href');"/>Java Collections</a>

I'm able to get the this message in alert window inside this method do_the_click but am not able to get the data in alert window, and so not able to get the page Java Collections.html in the index.html,

I have searched very much on Google re. how to load the data locally without using server in jquery.ajax but I didn't find any good solution, so I think that its good for others as well if it resides on Stack overflow.

I am running ajax without a server on my system, I have created one index.html with this.

JavaScript function:

function do_the_click(url)
{
alert('inside this method do_the_click');
    $.ajax({
                         async: false,
                         url : url, 
                         contentType: "text/html",
                         type : "GET",
                         dataType : "text/html",                
                         success: function(data){
                         alert('data');
           }});
} 

My HTML body content is:

<a href="Java Collections.html" class="button" id="javacollections" onclick="do_the_click('this.href');"/>Java Collections</a>

I'm able to get the this message in alert window inside this method do_the_click but am not able to get the data in alert window, and so not able to get the page Java Collections.html in the index.html,

I have searched very much on Google re. how to load the data locally without using server in jquery.ajax but I didn't find any good solution, so I think that its good for others as well if it resides on Stack overflow.

Share Improve this question edited Mar 28, 2021 at 1:06 Rachel Gallen 28.6k22 gold badges75 silver badges86 bronze badges asked Dec 29, 2013 at 19:59 GirishGirish 1,7171 gold badge19 silver badges34 bronze badges 3
  • @MikeW I would not agree to this. Making AJAX request without server is possible. I have done it several times. The browser makes the HTTP request on your behalf. – Talha Masood Commented Dec 29, 2013 at 20:04
  • I think you meant to use do_the_click(this.href) without the single quotes. The request may still fail because there is a space in the filename though, you probably want to get rid of the space. – Jarrett Widman Commented Dec 29, 2013 at 20:05
  • As long as there is no "cross domain ajax request" I don't see any reason why they can't make AJAX requests from a stand alone html page not running on any server. I have done it numerous times when developing HTML 5 apps for mobile. The app has to be piled in PhoneGap so all AJAX requests are made as it is. – Talha Masood Commented Dec 29, 2013 at 20:16
Add a ment  | 

7 Answers 7

Reset to default 4

You cannot do such thing. if you dont have a server you cannot send ajax it will be a cross browser issue. as the others says ajax will not work with file:// protocol you need a server to make an http:// call that ajax supports.

When you are making AJAX requests using stand alone html files, the HTTP request to the url is made by the browser. All you need to make sure is that you have included JQuery. Here is what you have to do.

Include Jquery:

<script src="js/jquery-1.10.2.min.js"></script>

The JQuery here resides in the js folder. This is included in the stand alone html file. Now to make the AJAX request use the following code.

$.ajax({
          type: 'GET',
          url:  url, 

          success: function(data){
              console.log(data);
          },
          async:false
      });

AJAX creates an HTTP request that must be responded to by a server. If you don't have a server you can't use an AJAX request. You can run a server on your puter, but this is not the same as getting a file from the local filesystem.

I m able to run the ajax without webserver the problem was with my code the code written below that i had run on my filesystem u can use that as well.

      function do_the_click(brl)
      {
          alert('inside this method do_the_click');
          alert(brl);
          var request  =     $.ajax({
                    async: false,
                    url: brl, 
                    contentType: "text/html",
                    type : "GET",
                    dataType : "html",                
                });

                request.done(function( msg ) {
                          alert(msg);
                          $( "#contentarea" ).load( msg, function() {
                                    alert( "Load was performed." );
                          });
                });

                request.fail(function( jqXHR, textStatus ) {
                          alert( "Request failed: " + textStatus );
                });

      }

Java Collections

You won't be able to do this. If you don't have a server, you will violate the browser's cross-domain policy, as you won't have a domain!

AJAX won't work via the filesystem, you need a server.

This is an older thread but a browser doesn't need to run on a mon OS like OSX/Windows/etc .. many people today are making their own browsers from WPE/Wayland or QT5Webkit where same origin policies don't apply.

Now in current scenario it depends upon where your server at. If the server is some place else, it will require CORS (cross origin request). So using file:// with CORS works perfectly.

I came to this question because I was searching for the reason, why I am able to make ajax request without using a web server. Got the answer Thanks!

发布评论

评论列表(0)

  1. 暂无评论