javascript - JSON 响应输出不会在单击事件时更新
问题描述
我正在尝试使用 django 构建一个 java 在线编译器 web 应用程序。但是每次我通过 AJAX 调用提交我的代码时,新代码都会成功发布到 view.py 但 JSON 响应对象仍然是前一个。
def code1(request):
response_data={}
**codet=request.POST.get("code","")** # getting the code text from Ajax req
**output=code(codet)** #sending code for execution
response_data['result']="Successful" #storing in response
response_data['message']=output
**return HttpResponse(json.dumps(response_data), content_type="application/json")**
def code(code_text):
return execute(code_text)
def execute(code_text):
filename_code=open('Main.java','w+') #creating file
filename_code.flush()
f1 = code_text.split("\n") # splitting code line by line
for i in f1:
filename_code.write(i+"\n") #writing code line by line
filename_code.close() #closing file
time.sleep(2) #giving wait
**compile_java(filename_code)**
#compiling file (Note: I am not using the file passed in compile_java)
**return execute_java(filename_code,input**) #running java file
def compile_java(java_file):
proc = subprocess.Popen('javac Main.java', shell=True)
def execute_java(java_file, stdin):
#java_class,ext = os.path.splitext(java_file)
cmd = ['java ', 'Main']
proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
stdout,stderr = proc.communicate(stdin)
stdoutstr= str(stdout,'utf-8')
**
1. return stdoutstr
**
实际上正在为当前代码(codet)发送先前的 json 响应
解决方案
推荐阅读
- php - 命令到处理程序以在 DDD 和 CQRS 中聚合根到存储库流
- python - PulpSolverError: Pulp: 执行 C:\Users\ipeki\anaconda3\lib\site-packages\pulp\apis\..\solverdir\cbc\win\64\cbc.exe 时出错
- git - 在提交更改时出现以下错误
- java - 从智能合约交易中获取解码输出
- c# - 使用该方法开发 C# 代码。开发一个单元测试以验证该方法是否有效
- javascript - 如何将具有多个值的对象添加到数组中?
- vue.js - Vue JS:注销后执行 router.push() 方法时,表单没有被清除
- r - 为什么我用 chromoqc 看不到我的变体?
- powershell - Set-SFTPFile 上传完整目录
- amazon-web-services - 如何知道之前使用的是哪个 AWS RDS 实例