Get方式传参
Django中的代码如下:
- urls.py代码:
from django.conf.urls import url
from django.contrib import admin
import AjaxTest.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^index/$",AjaxTest.views.index),
]
- views.py代码:
from django.http import HttpResponse
def index(req):
print req.GET.get('url')
if req.GET.get('url')=='test':
return HttpResponse("hello,this is a test")
else:
return HttpResponse("hahahaha")
jQuery中的代码如下:
- 方式1:
$("input").click(function() {
$.get("/index/?url=test", function (response, status, xhr) {
$(".box").html(response);
});
});
- 方式2:
$("input").click(function() { $.get("/index/", "url=test", function (response, status, xhr) { $(".box").html(response); }); });
- 方式3:
$("input").click(function() {
$.get("/index/",{
url:"test"
},function(response,status,xhr){
$(".box").html(response);
});
});
POST方式传参
Django中的代码如下:
- urls.py代码:
from django.conf.urls import url
from django.contrib import admin
import AjaxTest.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r"^index/$",AjaxTest.views.index),
]
- views.py代码如下:
def index(req):
if req.POST.get('url')=='test':
return HttpResponse("htllo,this is a test")
else:
return HttpResponse("hahahahah")
jQuery中的代码如下:
- 方式1:
$("input").click(function () {
$.post("/index/","url=test",function(response,status,xhr){
$(".box").html(response)
});
});
- 方式2:
$("input").click(function(){
$.post("/index/",{
url:"test"
},function (response,status,xhr) {
alert(status);
$(".box").html(response)
});
});
- 方式3:
$("input").click(function(){
$.ajax({
type:"POST",
url:"/index/",
data:{url:"test"},
success:function (response,status,xhr) {
$(".box").html(response);
}
})
});