English 中文(简体)
从 jQuery s '$.get' 中保存字符串
原标题:Save string from jQuery s `$.get`

我想从一个 URL 上加载一些内容, 并将其用在我的代码中。 我尝试关闭关闭, 并且这样:

function getStringFromURL(url) {
  var getter = function() {
    this.result = "undef";
    this.func = function(response) {
      this.result = response;
    };
  };
  var x = new getter();
  $.get(url, x.func);
  return x.result;  // the it returns "undef" and not the wanted response
}

Nothing worked at all. I ll never got the content, but if I call it with alert like $.get("http://localhost:9000/x", function(response) { alert(response) }); it works -- but I d like to save the response. I think theres a problem with the scope of the $.get-method.

这有什么错?

问题回答

您无法在没有服务器给予明确同意的情况下在标准获取查询中分析从其它域名或端口获取的内容 。

Read this : https://developer.mozilla.org/en/http_access_control You ll see how to define the proper header for your site so that it says the browser that cross-domain requests are fine.

您有一个关闭问题。 如果您想要在除抓取器以外的其它背景下调用 x. func, 请尝试这个 :

var getter = function() {
   var _this = this;
   this.result = "undef";
   this.func = function(response) {
      _this.result = response;
     };
 };

EDIT : 正如其他人提到的那样, 您无法立即返回 x. 由 < code> 获取 FromURL 的结果。 您必须在回调中使用该值 。 事实上, 更普遍地说, 无法在无同步调用的调用上定义同步抓取器 。

$. Get 是 Async 方法 。 get is Async 方法

您需要通过回调函数作为参数来获得 StringFrotURL

function getStringFromURL(url, callback) {
            var getter = function () {
                this.result = "undef";
                this.func = function (response) {
                    this.result = response;
                    callback(response);
                };
            };
            var x = new getter();
            $.get(url, x.func);
        }

getStringFromURL("http://localhost:9000/x", function (res) { alert(res) });

如果您想要返回结果, 这是不可能的 。

You can t mix synchronous and asynchronous in JavaScript if you block the script, you block the Browser.

请在此检查 < a href=" "https://stackoverflow.com/ questions/4288779/asynchronous-for- cound- in-javascript" > JavaScript 的周期同步





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签