jQuery:将 Ajax 获取到变量?

发布于 2024-10-10 20:10:25 字数 348 浏览 0 评论 0原文

我认为下面显示的代码可以发挥作用,但显然事实并非如此。我不知道为什么它不起作用 - URL 已正确获取(使用 firebug 检查),但它拒绝将结果保存到我的变量(globalVar)中。

我能做些什么?

var globalVar;  

function fetchDataset(kategori){  

    $.getJSON(apiUrl + "?jsoncallback=?", function(json){
        globalVar = json;

        console.log(globalVar); // Returns Undefined!
    });


}

I thought that the below shown code would work as a charm, but apparently it doesn't. I can't tell why it isnt working - The URL is fetched properly (checked with firebug) but it refuses to save the result to my variable (globalVar).

What can I do?

var globalVar;  

function fetchDataset(kategori){  

    $.getJSON(apiUrl + "?jsoncallback=?", function(json){
        globalVar = json;

        console.log(globalVar); // Returns Undefined!
    });


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

謸气贵蔟 2024-10-17 20:10:25

我想萤火虫应该显示你的回调的内容。我有一个类似的问题并使用 .parseJSON(); 解决了它

http://api.jquery.com/jQuery.parseJSON/

也许有帮助。

回调的 json 格式是否整齐/“正确”?

I guess firebug should display the contents of your callback. I had a similar problem and solved it using .parseJSON();

http://api.jquery.com/jQuery.parseJSON/

Maybe it helps.

Is the callback neatly formatted/"correct" json?

最丧也最甜 2024-10-17 20:10:25

您的变量不是全局的。您在函数的范围内定义了它。您的 Ajax 调用是异步运行的,这意味着您的函数将(可能)在 ajax 请求返回之前完成。尝试在函数外部声明 globalVar。 IE。

var globalVar;    
function fetchDataset(kategori){

编辑:此外,检查statusText(传递给回调函数的第二个变量)以确保您的请求没有错误。

Your variable is not global. You defined it within the scope of the function. Your Ajax call is run asynchronously which means that your function will complete (probably) before the ajax request is returned. Try declaring globalVar outside of the function. ie.

var globalVar;    
function fetchDataset(kategori){

Edit: Also, check the statusText (second variable passed to the callback function) to make sure that there are no errors with your request.

浅笑依然 2024-10-17 20:10:25

您是否正在发出跨域请求?

如果是这样,请确保您使用 jsonp

JSONP
如果 URL 包含字符串“callback=?” (或类似的,由服务器端 API 定义),请求将被视为 JSONP。有关更多详细信息,请参阅 $.ajax() 中对 jsonp 数据类型的讨论。

这里有一个 jsonp 的例子。
http://api.jquery.com/jQuery.getJSON/

Are you making a cross domain request?

If so make sure you use jsonp

JSONP
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

There is an example here of jsonp.
http://api.jquery.com/jQuery.getJSON/

唯憾梦倾城 2024-10-17 20:10:25

显然,没有任何效果,所以我最终制作了一个基于 PHP 的代理来传输任何跨域请求。将 url 更改为 PHP 代理后,效果很好。

感谢大家的帮助。非常感谢!

Apparently, nothing worked, so I instead ending up making a PHP-based proxy that tunnels any cross-domain request. After changing the url to the PHP proxy, it worked great.

Thanks for everyones help. Very appreciated!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文