Express sending back JSON via Ajax Get Issue

56 views
Skip to first unread message

A

unread,
Jul 1, 2015, 3:38:50 PM7/1/15
to nod...@googlegroups.com
HI,

I have a page that is requesting data via ajax as such

....
url
= 'hello/world/123',
method
: 'get',

...

The route is this
app.get('/hello/world/:num', function(req, res) {hello.readWorld(req, res, req.params.num)});



Which successfully calls this
module.exports.readWorld= function (req, res, num) {
    world
.findById(num, {'_id': 0,'Stuff' : 1}, function (err, results) {        
       
if (err) res.send(err);          
        res
.type('json');
        res
.json({"data":results});
       
//res.send(results);
   
});
}


The above works, i get all the correct JSON

The problem is it overwrites my whole page that initiates the ajax call with JSON data vs binding it to the call's response

I tried res.send(results) and a few other variations with no luck

Thanks

A

unread,
Jul 1, 2015, 11:39:44 PM7/1/15
to nod...@googlegroups.com
Forgot to mention, if it matters, that it is a JQuery operation

Satheesh Natesan

unread,
Jul 2, 2015, 9:31:20 AM7/2/15
to nod...@googlegroups.com
The problem is in the client code. What is the client code?

A

unread,
Jul 2, 2015, 11:28:44 AM7/2/15
to nod...@googlegroups.com
I am using JEasyUI

This same code works with ASP.net (no routes, just straight linking to a page).   I am just trying migrate to nodejs



<table id="dgSingle" style="width:100%;height:100%" class="datagrid">
   
<thead>
       
<tr class="datagrid-header">
           
<th data-options="field:'lhid',width:0, hidden: true"></th>
           
<th data-options="field:'Description'">Course Description</th>
           
<th data-options="field:'CId',width:120, hidden: true">Course ID</th>
           
<th data-options="field:'Completed',width:100">Completion Date</th>
           
<th data-options="field:'Hours',width:64, hidden: true">Total Hours</th>
           
<th data-options="field:'CreditHours',width:64">Contact Hours</th>
           
<th data-options="field:'ClassType',width:128, formatter:cellStyler">Class Type</th>
           
<th data-options="field:'Label',width:120, hidden: true">Label</th>
       
</tr>    
   
</thead>
</table>





<script type="text/javascript">
    $(function () {
        var dgS = $('#dgSingle').datagrid({
            url: '/hello/world/123',
            fitColumns: true,
            oneSelect: true,

            remoteSort: false,
            onHeaderContextMenu: function (e, field) {
                e.preventDefault();
                if (!cmenu) {
                    createColumnMenu();
                }
                cmenu.menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }, });

A

unread,
Jul 2, 2015, 1:25:00 PM7/2/15
to nod...@googlegroups.com
Just tried with plain ajax...get the same thing, the result overwrites the page with tis data and all I see is the valid json


$(function () {
        $
.ajax({
            type
: "GET",
            url
: '/hello/world/123',
            data
: '',
            contentType
: "application/json; charset=utf-8",
            dataType
: "json",
            success
: function (data) {
               
if (data.d.length > 0) {                
                    document
.getElementById("divTest").innerHTML = data;
               
}
               
else {
                    console
.log("Error at " & data.d.length);
               
}
           
},
            error
: function (data) {
                console
.log("Error: " & data.d);
           
}
       
});        
   
});

A

unread,
Jul 4, 2015, 11:31:33 AM7/4/15
to nod...@googlegroups.com
Found it, 

My route was calling the return JSON, versus the page itself that has the Jquery
Reply all
Reply to author
Forward
0 new messages