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条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP