【发布时间】:2012-06-29 21:39:32
【问题描述】:
虽然我有编程经验,但对 GS、JS 或任何与 UI 相关的东西完全陌生。
场景:从 Greasemonkey 脚本向 Servlet 进行 AJAX 调用
Greasemonkey/JS 代码:
function getResultsData(query){
alert("Getting the Data");
$.ajax(
{
cache: false,
data: {"q":query},
dataType:"text",
url: "http://myserver.com:8000/search?",
success: processData
}); //end of $.ajax }
function processData(data){
alert("Got the data");
var myResultDiv = document.getElementById("searchRes");
myResultDiv.innerHTML = data; }
Servlet 代码:
System.out.println("-----------This is an AJAX call------------------");
//Commented the original logic
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().write("Text from Servlet");
问题:
如果 url(在 $.ajax 中)是其他一些现有 API,则 GS/JS 代码可以完美运行。响应反映在 UI 中
但是,当我提供服务器的 url 时,我可以在 Firebug.Console 中观察到该调用没有 http 响应,但状态显示为 200 OK,整个条目变为“RED”。
当我测试从 Firebug 的“http 调用条目”复制的 url 时,它运行良好,因为我可以在新选项卡上看到响应“来自 Servlet 的文本”。
有人可以帮忙吗?
注意 运行greasemonkey的网站,我的服务器属于同一个域,即
Greasemonkey 网站:www.example.com
我的服务器:www.myserver.example.com
【问题讨论】:
-
你能把网址改成“搜索”而不是“myserver.com:8080/search”吗? ....还在您的 servlet 中添加另一行 ... resp.getWriter().flush()
-
感谢您的回复。两个都试了,都不行。我想,你的意思是 - “myserver.com:8080/search”
-
跳过 myserver.com:8080 部分...只需给出相对 URL
-
您通常不能对另一台服务器进行 ajax 调用。这被阻止以防止跨站点脚本攻击。尽管考虑到您不能在用户访问的页面上放置任何服务器端代码,但我不确定您将如何在greasemonkey 脚本中与另一台服务器通信...
-
@Firefox 否,浏览器会阻止请求。如果是 javascript,请记住,这一切都发生在客户端。另外,我不是 100% 确定,但我认为就浏览器而言,
myserver.example.com和example.com是不同的服务器
标签: javascript jquery ajax servlets greasemonkey