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

报告相同问题?

悬赏问题

  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗