XMLHTTPREQUEST到Python脚本网络错误

时间:2013-12-17 10:12:47

标签: javascript python xmlhttprequest

我在apache 2.2中有这个python脚本

#!/Python27/python


import sys, json

# Parse input into a json object
myjson = json.load(sys.stdin)

#get values
num1 = float(myjson['num1'])
num2 = float(myjson['num2'])
type = myjson['type']

#perform cal based on selection 
if type =="Add":
    total = num1 + num2
else:
    total = num1 - num2 



print'Content-Type: application/json\n\n'
print json.dumps( {'ans':total}) 

我尝试执行xmlhttprequest,但遇到网络错误。为什么呢?

//Address to post to 
var url = "http://localhost/demo/calculate.py";

//Create json object
var json = '{"num1":' + num1 + ', "num2":' + num2 + ',"type":"' + type + '"}';

var output = document.getElementById("updateMe");
output.innerHTML += "send";

try {

    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, false)
    xhr.send(json);
} catch(e) {
    alert(e);
}

0 个答案:

没有答案