简体   繁体   English

Javascript在本地运行良好,但不在我的服务器上

[英]Javascript works great locally, but not on my server

I'm teaching myself javascript by creating a script for displaying an external rss feed on a webpage. 我通过创建用于在网页上显示外部RSS提要的脚本来自学javascript。

The code I patched together works great locally. 我修补的代码在本地运行很好。 This is a screen grab of the code producing exactly the desired behavior. 这是代码的屏幕抓取,产生完全所需的行为。 The code is populating all the information inside the section "Blog: Shades of Gray", except for "tagged" which I hard coded: 代码填充了“Blog:Shades of Grey”部分中的所有信息,除了我标记的“已标记”:

http://screencast.com/t/fNO5OPnmGPm2

But when I upload the site files to my server, the code doesn't work at all. 但是当我将站点文件上传到我的服务器时,代码根本不起作用。 This is a screen grab of the code on my site NOT producing the desired behavior... 这是我网站上的代码的屏幕抓取没有产生所需的行为......

替代文字

This feels like I'm not getting something really basic about how javascript works locally vs. on the server. 这感觉就像我没有得到关于javascript如何在本地和服务器上工作的真正基本的东西。 I did my half hour of googling for an answer and no trails look promising. 我做了半小时的谷歌搜索,没有看起来很有希望。 So I'd really appreciate your help. 所以我非常感谢你的帮助。

This is my site (under construction) http://jonathangcohen.com 这是我的网站(正在建设中) http://jonathangcohen.com

Below is the code, which can also be found at http://jonathangcohen.com/grabFeeds.js . 以下是代码,也可以在http://jonathangcohen.com/grabFeeds.js找到。

/*Javascript for Displaying an External RSS Feed on a Webpage
Wrote some code that’ll grab attributes from an rss feed and assign IDs for displaying on a webpage. The code references my Tumblr blog but it’ll extend to any RSS feed.*/

window.onload = writeRSS;

function writeRSS(){
    writeBlog();
}

function writeBlog(){
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","http://blog.jonathangcohen.com/rss.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    var x=xmlDoc.getElementsByTagName("item");
    //append category to link
    for (i=0;i<3;i++)
      {     
      if (i == 0){
          //print category
          var blogTumblrCategory = x[i].getElementsByTagName("category")[0].childNodes[0].nodeValue
          document.getElementById("getBlogCategory1").innerHTML =  
         '<a class="BlogTitleLinkStyle" href="http://blog.jonathangcohen.com/tagged/'+blogTumblrCategory+'">'+blogTumblrCategory+'</a>';
          //print date
          var k = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue
          thisDate = new Date();
          thisDate = formatTumblrDate(k);
          document.getElementById("getBlogPublishDate1").innerHTML = thisDate;
          //print title
          var blogTumblrTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
          var blogTumblrLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue
          document.getElementById("getBlogTitle1").innerHTML =
          '<a class="BlogTitleLinkStyle" href="'+blogTumblrLink+'">'+blogTumblrTitle+'</a>';
          }
     if (i == 1){
        //print category
        var blogTumblrCategory = x[i].getElementsByTagName("category")[0].childNodes[0].nodeValue
        document.getElementById("getBlogCategory2").innerHTML = 
        '<a class="BlogTitleLinkStyle" href="http://blog.jonathangcohen.com/tagged/'+blogTumblrCategory+'">'+blogTumblrCategory+'</a>';
        //print date
        var k = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue
        thisDate = new Date();
        thisDate = formatTumblrDate(k);
        document.getElementById("getBlogPublishDate2").innerHTML = thisDate;
        //print title
        var blogTumblrTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
        var blogTumblrLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue
        document.getElementById("getBlogTitle2").innerHTML =
        '<a class="BlogTitleLinkStyle" href="'+blogTumblrLink+'">'+blogTumblrTitle+'</a>';
        }
     if (i == 2){
        //print category
        var blogTumblrCategory = x[i].getElementsByTagName("category")[0].childNodes[0].nodeValue
        document.getElementById("getBlogCategory3").innerHTML = 
        '<a class="BlogTitleLinkStyle" href="http://blog.jonathangcohen.com/tagged/'+blogTumblrCategory+'">'+blogTumblrCategory+'</a>';
        //print date
        var k = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue
        thisDate = new Date();
        thisDate = formatTumblrDate(k);
        document.getElementById("getBlogPublishDate3").innerHTML = thisDate;
        //print title
        var blogTumblrTitle = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue
        var blogTumblrLink = x[i].getElementsByTagName("link")[0].childNodes[0].nodeValue
        document.getElementById("getBlogTitle3").innerHTML =
        '<a class="BlogTitleLinkStyle" href="'+blogTumblrLink+'">'+blogTumblrTitle+'</a>';
        }
      }
}

function formatTumblrDate(k){
    d = new Date(k);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    curr_month++;
    var curr_year = d.getFullYear();
    printDate = (curr_month + "/" + curr_date + "/" + curr_year);
    return printDate;
}

Thank you! 谢谢!

AJAX runs on a same-origin policy, ie, you can only call your own server. AJAX运行在同源策略上,即您只能调用自己的服务器。

A fix would be making a call to your server which in turn calls the target server. 修复程序将调用您的服务器,而服务器又调用目标服务器。

Here's how (to give you a general picture ;)) 这是如何(给你一般情况;))

替代文字

You are doing an XmlHttpRequest to another domain, crossing the security sandbox and going against the same-origin policy, which means the request to blog.jonathangcohen.com/rss.xml fails, and you get nothing out of it. 您正在对另一个域执行XmlHttpRequest,跨越安全沙箱并违反同源策略,这意味着对blog.jonathangcohen.com/rss.xml的请求失败,并且您没有得到任何结果。

The only viable solutions are to either use jsonp or a proxy on your jonathangcohen.com domain, for example a simple php script that would just contain the following would do the trick: 唯一可行的解​​决方案是在jonathangcohen.com域上使用jsonp或代理,例如,只包含以下内容的简单php脚本可以解决这个问题:

<?php
header('Content-Type: text/xml');
echo file_get_contents('http://blog.jonathangcohen.com/rss.xml');

Then you request the data from http://jonathangcohen.com/rss-proxy.php 然后从http://jonathangcohen.com/rss-proxy.php请求数据

This seems like a permissions thing. 这似乎是权限的事情。 Cross-site scripting and all. 跨站点脚本和所有。 The browser is more lenient on local pages (probably) but won't allow you to do this on an actual server. 浏览器在本地页面上可能更宽松(可能)但不允许您在实际服务器上执行此操作。 You'll need to grab that data on the server and then feed it to your javascript. 您需要在服务器上获取该数据,然后将其提供给您的javascript。

It's a cross-origin policy thing, which you can read more about here: http://arunranga.com/examples/access-control/ 这是一个跨域策略的东西,你可以在这里阅读更多: http//arunranga.com/examples/access-control/

In order for javascript on mydomain.com to fetch resources from blog.mydomain.com, blog.mydomain.com will need to send the response header Access-Control-Allow-Origin: http://mydomain.com 为了让mydomain.com上的javascript从blog.mydomain.com获取资源,blog.mydomain.com将需要发送响应头Access-Control-Allow-Origin: http://mydomain.com //mydomain.com

The only other way to do it that I know of would be to setup a script on blog.mydomain.com, such as blog.mydomain.com/feed.php, that will return a valid JSONP response. 我知道的另一种方法是在blog.mydomain.com上设置脚本,例如blog.mydomain.com/feed.php,它将返回有效的JSONP响应。 In order to use it, you would then, instead of using XMLHttpRequest, create a <script> element, and set the source to http://blog.mydomain.com/feed.php . 为了使用它,您可以创建<script>元素,而不是使用XMLHttpRequest,并将源设置为http://blog.mydomain.com/feed.php The output from feed.php should call a javascript function and pass in the actual content of the XML feed, if that makes sense. feed.php的输出应该调用javascript函数并传入XML feed的实际内容,如果这是有意义的。

edit: The other answers will obviously work as well, and the specific answer about using a proxy script on your site (that reads in and spits out the content of the feed) would be even easier and would only require a URL change in your existing javascript. 编辑:其他答案显然也会起作用,关于在您的网站上使用代理脚本(读入并吐出Feed的内容)的具体答案将更加容易,并且只需要对现有内容进行URL更改JavaScript的。

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

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