数据未根据Ajax请求发送回

时间:2018-07-18 16:47:01

标签: php ajax

我正在向服务器发送AJAX请求,以在数据库中创建一个新条目,然后返回创建的条目的ID。运行代码时,控制台会显示#Initialize list to hold all of the accuracy data all_accuracies = [] #For each training set group for group_number in range(len(train_set_sizes)): #For each ANN in training set group for i in range(num_ANNs): #Initialize group_accuracies list group_accuracies = [] #--------------Generate a training set-------------------------------- #Initialize train_set lists train_set_examples = [] train_set_labels = [] #randomly select indices to pull train set examples example_select_indices = np.random.randint(0,55000,size=train_set_sizes[group_number]) #For number of images in the appropriate training set group for j in range(train_set_sizes[group_number]): random_index = example_select_indices[j] train_set_examples = np.asarray(train_set_examples.append(train_images[random_index])) train_set_labels = np.asarray(train_set_labels.append(train_labels[random_index]) # Create the MNIST Estimator mnist_classifier = tf.estimator.Estimator(model_fn=cnn_model_fn) # Train the CNN model mnist_classifier.train(input_fn=train_input_fn,steps=train_steps) # Evaluate the models and print results eval_result = mnist_classifier.evaluate(input_fn=eval_input_fn) print("Group Number ",group_number+1,", ANN #",i+1," Accuracy: ",eval_result," %") #Record ANN accuracy group_accuracies.append(eval_result) #record group accuracies in all_accuracies all_accuracies.append(group_accuracies) 未定义。知道为什么吗?这是我的代码:

客户

response

服务器:

var tempId;

     var request = $.ajax({
        type: 'POST',
        datatype: 'json',
        data: {""},
        url: 'file.php?recID=' + recID,
        success: function(response) {
            console.log(response);
            tempId = response.recentId;
        }
    });

1 个答案:

答案 0 :(得分:1)

我刚刚意识到我原来的答案是错误的,因为这样success回调根本不会被称为

实际的问题是您要返回status code 204,表示“无内容”。 $.ajax()(和/或底层XMLHttpRequest)对此表示尊重,并且不会处理任何可用的正文数据。


原始答案:

如果禁用output buffering,则在发送完任何正文数据后就无法设置HTTP响应代码,因为HTTP响应标头不能在HTTP响应正文之后发送。

尝试在发送任何数据之前设置HTTP响应代码 ,例如:

http_response_code($code);
echo json_encode($fdArray);

很可能您的脚本中还有其他错误。例如,$recID来自哪里?查看您的错误日志。


作为旁注,为什么还不将$recID绑定到准备好的语句,而不是直接将其插入查询字符串?这是一个安全问题。您已经在使用准备好的语句,因此请正确使用它们。 :)