javascript - Ajax 结果在 alert() 中

标签 javascript php jquery ajax

<分区>

在 $.ajax() 之外使用时,如何让 imageX 显示它的值(value)?

var imageX;
$.ajax({
    type: 'GET',
    url: 'php/my1.php',
    dataType: 'json',
    async: 'false',
    success: function(response){
        imageX = response[0].studentName,
        groupX = response[0].subjectId;
    }
});
alert(imageX);

最佳答案

改变

异步: 'false',

异步:错误,

'false' 是由 Jquery(JS) 转换为 true 的字符串。

Jquery Ajax 文档说异步接受 bool 值。

async (default: true): Type: Boolean By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false.

https://api.jquery.com/jQuery.ajax/

异步:错误 将导致 jQuery.ajax() 调用阻塞,直到它返回。 而不是这个:

function your ajax_calling_function
    call your ajax 
    callback with results

关于javascript - Ajax 结果在 alert() 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21903019/

相关文章:

javascript - 如何将我的数据传递到 google 图表 api?

javascript - 使用 JSONP 加载 html 页面

php - 如何将 MM/DD/YYYY 转换为 YYYY-MM-DD?

php - Laravel:如何修改通知集合

javascript - 多重提交按钮和回车键

javascript - 如何使用 Enter 键按下按钮

javascript - 如何在 Angular JS 中获得自动完成功能?

javascript - 强制 Google Maps API 仅使用纬度/经度值 - 修改预制 JS

javascript - 谷歌地图,隐藏山峰名称

jquery - 如何将类添加到高度大于宽度的元素的父级