这是来自html页面的ajax调用:
$('#b2').click(function() {
$.ajax({
url : "../../cgi-bin/testjson.py",
type : "post",
datatype : "json",
data : {
ac_number : $("#account_number").val()
},
success : function(response) {
var handle = document.getElementById("displaytext");
handle.innerHTML += '<p> Button clicked</p>';
handle.innerHTML += '<p> Value received is: </p>' + response.data[0];
}
});
});
答案 0 :(得分:3)
在python文件中:
import sys
import cgi
sys.stdout.write("Content-Type: application/json")
sys.stdout.write("\n")
sys.stdout.write("\n")
form = cgi.FieldStorage()
sys.stdout.write(json.dumps({ 'data': form.getvalue('ac_number')}))
答案 1 :(得分:0)
在django中,您可以访问request.POST
:
urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('',
url(r'^testjson', 'testjson.views.testjson'),
)
views.py
from django.shortcuts import HttpResponse
def testjson(request):
ret = "not a post"
if request.method == 'POST':
ret = str(request.POST)
return HttpResponse(ret)