0

I need a way to parse my JSONP object on server side to save it, due to cross domain origin issue I have shifted my way of communication from JSON to JSONP but not finding any suitable way to parse JSONP on server side to save it to the database.

Following is the Model,

define(['backbone'],function(Backbone){
  'use strict';

    return Backbone.Model.extend({
        url:"http://crossdomain:9847/page",
        defaults: {
                type:'text',
                position:0,
                align:'left',
                text:{"en":""},
                color:"#000",
                weight:'normal',
                size:"14px",
                font:"Verdana",
                pageid:'askdkasdkgaskdgks'

        },
        idAttribute:'_id',
        sync: function(method, collections, options) {
            options.dataType = "jsonp";
            return Backbone.sync(method, collections, options);
          }
    });

});

Express Server,

var express = require('/root/node_modules/express');

var page = require('./routes/page.js');

var app = express();

app.configure(function () {
    app.use(express.json());
    app.use(express.urlencoded());
    app.set("jsonp callback", true);
})

app.get('/page', page.updatePage);

app.listen(9847);

exports.updatePage = function(req, res) {
    console.log(req.query);
    // Here how I can parse the req is my problem 
    // so I can save object to database?
}

URL is generating like,

http://crossdomain:9847/page?callback=jQuery203010156283635587138_1384408493698&{%22text%22:{%22en%22:%22Text%22},%22type%22:%22text%22,%22position%22:0,%22align%22:%22left%22,%22color%22:%22#000%22,%22weight%22:%22normal%22,%22size%22:%2214px%22,%22font%22:%22Verdana%22,%22pageid%22:%22askdkasdkgaskdgks%22}&_=1384408493700

and I am able to receive,

{ callback: 'jQuery203010156283635587138_1384408493698',
  '{"text":{"en":"Text"},"type":"text","position":0,"align":"left","color":"': '' }

Now how can I parse this ? I can get callback from callback parameter, but how to get actual data ?

Damodaran
  • 10,882
  • 10
  • 60
  • 81
HRK
  • 287
  • 2
  • 15

1 Answers1

0

You can't parse the result because it's not valid JSON. Your problem is probably in this line:

app.set("jsonp callback", true);

This is where you set the JSONP callback variable, for example changing it from the default of callback to instead be callbackVariable.

Just comment out that line, and the JSONP you get back will hopefully be parseable. Or, you might also have to fix how Backbone is constructing the JSONP URL. If you instead used a URL like http://crossdomain:9847/page?callback=jQuery203010156283635587138_1384408493698&type=text&position=0&align=left&color=%23000&weight=normal&size=14px&font=Verdana&pageid=askdkasdkgaskdgks I believe it would work. Backbone seems to be adding additional encoding into the values in the URL, which makes parsing harder.

Finally, if you need help easily picking specific values out of a (valid) JSON string that has been parsed into a Javascript object, take a look at the many useful function in lodash.

Dan Kohn
  • 33,811
  • 9
  • 84
  • 100
  • thanks for reply, I have commented out the line and also tried to assign original callback variable like "jQuery20301659487988231888_1385372097880" but still there is no luck, do you have any other suggesions? – HRK Nov 25 '13 at 09:36
  • 1
    Looking at it again, I'm pretty sure the bug is on the Backbone side. You're constructing a really crazy piece of JSON where the key (not the value of the second item is: `{"text":{"en":"Text"},"type":"text","position":0,"align":"left","color":"`. Try drastically simplifying what you have on the client to just upload `foo: bar`. – Dan Kohn Nov 25 '13 at 15:16
  • I have simplify my model but result is still the same now I have following URL `http://192.168.20.62:9847/page?callback=jQuery20309653310032985027_1385469338355&{%22key%22:%22value%22}&_=1385469338356` and request.query as `{ callback: 'jQuery20309653310032985027_1385469338355', '{"key":"value"}': '', _: '1385469338356' }` – HRK Nov 26 '13 at 12:37
  • 1
    I'm 99% sure your problem is in your Backbone not your Express code. Could you please look for standard Backbone examples that generate a URL more like `http://192.168.20.62:9847/page?callback=jQuery20309653310032985027_138546933835‌​&key=value&_=1385469338356`. For example, http://stackoverflow.com/questions/9093209/new-backbone-model-vs-backbone-model-extend – Dan Kohn Nov 26 '13 at 14:03
  • Just to update you, still I am not able to resolve the issue, but the things are started from this issue http://stackoverflow.com/questions/19947948/apache-and-nodejs-cross-domain-ajax-issue and after good workout I have make things running. – HRK Dec 02 '13 at 05:25