down_load1117 2010-10-26 14:11
浏览 55
已采纳

bookmarklet中的Ajax:已完成操作但未给出答案

I'm trying to do my own bookmarklet and I already tried to read some response in SO but nothing to answer the weird reaction I got from my script.

I'm doing an AJAX call from my bookmarklet, so I do the little trick :

var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = "http://example.com/urlToMyJS.js";
document.body.appendChild(newScript);
void(0);

And the urlToMyJS.js is like this :

var u = 'http://example.com/scriptToCall.php';
var request = new XMLHttpRequest();
request.open("GET", u, true);
request.onreadystatechange = function() {
  var done = 4, ok = 200;
  if (request.readyState == done && request.status == ok) {
    if (request.responseText) {
      alert(request.responseText);
    }
  }
};
request.send(null);

The weird part is :

  • The javascript is always launched and scriptToCall.php is always called too (it logs every hit)
  • The alert shows the responseText when I click on the bookmarklet on example.com
  • Sometimes, on other sites, the alert shows nothing (but still appears)
  • Some other times, the alert doesn't even show... (but I still have the log hit...)

Do you have any idea why it does that? And if yes, do you have any idea how I could make it always show the responseText?

  • 写回答

2条回答 默认 最新

  • dpowhyh70416 2010-10-26 15:21
    关注

    Well, finally, I used another trick :

    var newScript = document.createElement("script");
    newScript.type = "text/javascript";
    newScript.src = "http://example.com/scriptToCall.php";
    document.body.appendChild(newScript);
    void(0);
    

    This way (the PHP is sending a javascript header), no more AJAX. It was nonsense in my case since both file were in the same server/folder, 1 movement instead of 2!

    Anyway, thanks bobince for all the details I might use in the future !

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)