2013-05-29 6 views
7

के लिए एक साधारण काम करने वाले AJAX उदाहरण की आवश्यकता है मूल रूप से मुझे django में http://www.w3schools.com/jquery/jquery_ajax_get_post.asp के समान कुछ चाहिए। मैंने नमूने डाउनलोड किए हैं और इसे स्थानीयहोस्ट + php के साथ स्थानीय रूप से परीक्षण किया है और यह ठीक काम करता है लेकिन मुझे लगता है कि यह django में काम करने के लिए प्रतीत नहीं होता है, उदाहरण के लिए यह कितना आसान है।django रूपों

<script type="text/javascript"> 
$(document).ready(function(){ 
    $("#my_form").submit(function(){ 
    $.post("", 
    {name:"Donald Duck", 
    city:"Duckburg"}, 
    function(data,status){ 
     alert("Data: " + data + "\nStatus: " + status); 
    }) 
    .fail(function() { alert("error"); }); 
    return false; 
    }); 
}); 
</script> 

यूआरएल:

url(r'^ajax/$', views.ajax_test, name="ajax"), 

विचार:

def ajax_test(request): 
    if request.method == 'POST' and request.is_ajax(): 
     name = request.POST['name'] 
     city = request.POST['city'] 
     message = name + ' lives in ' + city 

     return HttpResponse(json.dumps({'message': message})) #tried without the json. Doesn't work either 

    return render(request, 'books/ajaxTest.html') 
यहाँ मूल रूप से मैं क्या मामूली संशोधन

जावास्क्रिप्ट के साथ ऊपर के लिंक से उदाहरण के आधार पर किया गया है

एचटीएमएल:

<form id="my_form" action="" method="post" {% if form.is_multipart %}enctype="multipart/form-data"{% endif %}>{% csrf_token %} 
<input type="submit" value="Send"> 
</form> 

प्रपत्र एक डीजेंगो फॉर्म शामिल करने का अनुमान है, लेकिन चूंकि मैं मूलभूत काम भी नहीं कर सकता, यह थोड़े व्यर्थ होगा। किसी ने csrf_token टैग के बारे में उल्लेख किया है लेकिन इसे हटाने से समस्या हल नहीं होती है। उपर्युक्त उदाहरण का आउटपुट मूल रूप से केवल अलर्ट ('त्रुटि') उत्पन्न करता है और कुछ भी नहीं। मैं इतने सारे उदाहरणों से गुजर चुका हूं लेकिन मुझे सबसे बुनियादी बातों को

+0

यदि यो आप क्रोम इंस्पेक्टर या फ़ायरफ़ॉक्स फ़ायरबग में अपनी अजाक्स प्रतिक्रिया का निरीक्षण करते हैं, आपको कुछ त्रुटि संदेश देखना चाहिए जो आपको इस बात को समझने के लिए प्रेरित करेगा कि इसे कैसे ठीक किया जाए। –

+1

मैंने JQuery का उपयोग करके Django में अजाक्स को लागू करने पर एक ट्यूटोरियल लिखा: https://aliteralmind.wordpress.com/2014/09/21/jquery_django_tutorial/ – aliteralmind

उत्तर

8

ठीक है..एक्सएक्स आपकी टिप्पणियों के लिए .. मुझे यह सब ठीक हो गया है .. मूल रूप से मैं बस { % csrf_token%} और csrfmiddlewaretoken: '{{csrf_token}}' .. सिर्फ उन के लाभ के जो this..the नए कोड को पढ़ने के इस

तरह जावास्क्रिप्ट कुछ ऐसा दिखाई देगा हो सकता है के लिए:

<script type="text/javascript"> 
$(document).ready(function(){ 

    $("#my_form").submit(function(){ 
    $.post("", 
    {name:"Donald Duck", 
    city:"Duckburg", 
    csrfmiddlewaretoken:'{{ csrf_token }}' 
    }, 
    function(data,status){ 
     alert("Data: " + data + "\nStatus: " + status); 
    }) 
    .fail(function(xhr) { 
     console.log("Error: " + xhr.statusText); 
     alert("Error: " + xhr.statusText); 
    }); 
    return false; 
    }); 

}); 
</script> 

एचटीएमएल:

<form id="my_form" action="" method="post">{% csrf_token %} 
<input type="submit" value="Send"> 
</form> 
+1

नाइस। इसे पोस्ट करने के लिए धन्यवाद। – Paul

संबंधित मुद्दे