0

I am trying to get and post data from a listbox using the onchange= but i get the error Reference Error: Cant find variable: getMessage getMessage is the name of function that contains the ajax get and post and I'm gessueing it means that it can't find the function for some reason. does anyone know why? my code is shown below.

<script type="text/javascript"> 
    function getMessage() {
    var data = { message : {$("#messageselect").val(); }}; 

    $.ajax({
        url: "/message",
        type: "GET",
        data: JSON.stringify(data),
        dataType: 'jsonp'
    })

    $.ajax({
        url: "/message",
        type: "POST",
        dataType: 'json',
        contentType: 'application/json',
        success: function(data) {
            console.log(data);
        }
        error : function(err) {
            console.log("error fetching message");
        }
    });
 }
</script>

server:

app.post('/message', function(req, res) {
    console.log(JSON.stringify(req.body));
    Message.findOne({ 'page.message' : req.data }, function(err, message) {    
        if(err){
            throw err;
        }           
        res.send(message);
    });
});

html:

<form method="POST">
  <select multiple="multiple" class="messageselect" onchange="getMessage()" id="messageselect">
    <% if(message) { %>
        <% for(i=messagecount-1;i>=0;i--) { %>
            <option value="<%= message[i].page.message %>">
                 From: <%= message[i].page.username %> 
                 Message: <%= message[i].page.messagetitle %>
            </option>
        <% } %>
    <% } %>
    </select><br><br><br>
</form>

2 Answers 2

1

Syntax error in your function. Try with this.

function getMessage() {

    var data = {
        message: $("#messageselect").val()
    };


    $.ajax({
        url: "/message",
        type: "GET",
        data: JSON.stringify(data),
        dataType: 'jsonp'
    });

    $.ajax({
        url: "/message",
        type: "POST",
        dataType: 'json',
        contentType: 'application/json',
        success: function (data) {
            console.log(data);
        },
        error: function (err) {
            console.log("error fetching message");
        }
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please be more specific, nobody wants to run a diff-scan on this to find the character you've changed
comma needed after: success: function (data) { console.log(data); }
0

you need to define your function differently, like this:

var getMessage = function() {

...

}

or....

perhaps I am wrong here. for some reason i thought you needed to assign the function to a variable name in order to reference it. My next guess would be that the code which defines your function is somehow not getting into the page. You should use your developer tools (i use the tools in chrome but whatever browser you prefer) and see if you can actually locate the function in the source code of your page.

3 Comments

I'm just wondering how does that differ ? I've never used this method of function definition and didn't face any problem in similar cases.
if getMessage was an anonymous IIFE, this would be right, howeverm the function is defined. Setting it's reference to a variable changes nothing except the namespace
I think i see the issue. your missing a comma after the "success" parameter in your ajax json.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.