खोज…


AJAX का एक सरल कार्यान्वयन

आपके पास मूल एक्सप्रेस-जनरेटर टेम्पलेट होना चाहिए

App.js में, जोड़ें (आप var app = express.app() ) के बाद इसे कहीं भी जोड़ सकते हैं:

app.post(function(req, res, next){
    next();
});

अब अपनी index.js फ़ाइल (या इसके संबंधित मैच) में, जोड़ें:

router.get('/ajax', function(req, res){
    res.render('ajax', {title: 'An Ajax Example', quote: "AJAX is great!"});
});
router.post('/ajax', function(req, res){
    res.render('ajax', {title: 'An Ajax Example', quote: req.body.quote});
});

ajax.jade / ajax.pug या ajax.ejs फ़ाइल /views निर्देशिका बनाएं, जोड़ें:

जेड / पगजे के लिए:

extends layout
script(src="http://code.jquery.com/jquery-3.1.0.min.js")
script(src="/magic.js")
h1 Quote: !{quote}
form(method="post" id="changeQuote")
    input(type='text', placeholder='Set quote of the day', name='quote')
    input(type="submit", value="Save")

ईजेएस के लिए:

<script src="http://code.jquery.com/jquery-3.1.0.min.js"></script>
<script src="/magic.js"></script>
<h1>Quote: <%=quote%> </h1>
<form method="post" id="changeQuote">
    <input type="text" placeholder="Set quote of the day" name="quote"/>
    <input type="submit" value="Save">
</form>

अब, magic.js /public नामक एक फाइल magic.js

$(document).ready(function(){
    $("form#changeQuote").on('submit', function(e){
        e.preventDefault();
        var data = $('input[name=quote]').val();
        $.ajax({
            type: 'post',
            url: '/ajax',
            data: data,
            dataType: 'text'
        })
        .done(function(data){
            $('h1').html(data.quote);
        });
    });
});

आखिर तुमने इसे हासिल कर ही लिया है! जब आप सहेजें पर क्लिक करेंगे तो बोली बदल जाएगी!



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow