如标题所述,我有一个ajax电话。在成功函数上,我想将返回的数据存储到变量中,以便在我的javascript中使用。 randNum.php只是每2秒返回一个随机数,我想将这个数字用于我脚本中的其他函数。如何在我的javascript中使用从php文件发回的数据? 我知道有更合理的方法可以解决这个问题,但想知道如何以这种方式完成任务。
var result;
var interval = 2000;
function myCall() {
var request = $.ajax({
url: "randNum.php",
type: "GET",
dataType: "html",
success:function (msg) {
result = msg; //Not working as I intend
setTimeout(myCall, interval);
}
});
}
function(){
do something with result;
}
答案 0 :(得分:0)
在函数之外声明一个全局变量,并在ajax响应之后为其分配响应变量。
var results;
function TestJSONP(){
$.ajax({
url: "randNum.php",
jsonp: "callback",
dataType: "jsonp",
success: function (response) {
console.log(response);
results = response;
}
});
}
答案 1 :(得分:0)
您可能不需要指定import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4 import uic
from PyQt4.QtGui import QImage
import qimage2ndarray
import matplotlib
from matplotlib import colors as C
from matplotlib import pyplot, cm
from matplotlib.figure import Figure
import dicom
import numpy
import ReadDicom as RD
import Matrixhandler as MH
form_class = uic.loadUiType('Dicom_GUI.ui')[0]
class Mainwindow(QMainWindow, form_class):
def __init__(self):
super().__init__()
self.setupUi(self)
self.connect(self.CTREAD, SIGNAL("clicked()"), self.ct_clicked)
self.connect(self.STRUCTREAD, SIGNAL("clicked()"), self.st_clicked)
self.CTSlider.valueChanged.connect(self.valchange)
def ct_clicked(self):
self.dcmList = []
self.CTList = []
self.dcmList = RD.dcmRead()
RD.dSave(self.dcmList)
self.dcmList = RD.dcmReadAll()
self.CTList = RD.readCTdcm(self.dcmList)
dsCT1 = dicom.read_file(self.CTList[0])
self.x, self.y, self.z, self.dx, self.dy, self.dz, self.ConstPixelDims, self.ConstdoseDims, self.ConstdoseDimsalt, self.ConstPixelSpacing, self.ConstDoseSpacing = MH.GetCoordi(self.CTList)
self.ArrayCT = numpy.zeros(self.ConstPixelDims, float)
self.ArrayCT = MH.GetCTImg(self.CTList, self.ArrayCT)
self.CTSlider.setMinimum(0)
self.CTSlider.setMaximum(len(self.CTList)-1)
def st_clicked(self):
print('st clicked')
def valchange(self):
self.vals = self.CTSlider.value()
self.Temparray = self.ArrayCT[:,:,self.vals]
self.imgsh = qimage2ndarray.gray2qimage(self.Temparray)
self.pixemap = QPixmap.fromImage(self.imgsh)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = Mainwindow()
myWindow.show()
app.exec_()
,但可以使用dataType
来获取原始响应。像
jqXHR.responseText
当您设置function myCall() {
var request = $.ajax({
url: "randNum.php",
type: "GET",
success:function (data, textStatus, jqXHR) {
result = jqXHR.responseText;
setTimeout(myCall, interval);
}
});
}
并且响应不是该类型时,则ajax未成功,并且将调用dataType
回调。