Accessing CB Lite via jQuery

29 views
Skip to first unread message

David Maier

unread,
Dec 9, 2014, 10:45:43 AM12/9/14
to mobile-c...@googlegroups.com
Hi, 

just playing around with CBLite for Phonegap. I already was able to get the URL of the local CB Lite instance and also could create database. But if I want to PUT a document, then I am getting a 'Bad Gateway' error.

The example source code is available here:


Any idea what I am doing wrong?

Regards, Davi

Jens Alfke

unread,
Dec 9, 2014, 11:41:56 AM12/9/14
to mobile-c...@googlegroups.com
What's the exact URL of the PUT, the exact JSON body, and the exact error message?

—Jens

David Maier

unread,
Dec 11, 2014, 12:41:58 PM12/11/14
to mobile-c...@googlegroups.com
Hi Jens,

I investigated the CBLite log files and found out that it is an issue with jQuery escaping characters. So if you pass a JSON object as data it is actually handling it as a String by escaping E.G the brackets. You can avoid it by using:

function doPut(purl, pdata, callback, errCallback)
{
    var request = {
        url: purl,
        processData : false,
        type: 'PUT',
        data: pdata,
        contentType: "application/json",
        success: callback,
        error: errCallback
    };
    
    log("Requesting " + JSON.stringify(request)); 

    $.ajax(request);
}

So the  'processData : false' helped me in this case. Also I then had to pass my JSON data over as string directly by using JSON.stringify. So the solution for me is actually:

/**
 * Helper to execute a HTTP PUT
 */
function doPut(purl, pdata, callback, errCallback)
{
    var strdata = JSON.stringify(pdata);
    
    var request = {
        url: purl,
        processData : false,
        type: 'PUT',
        data: strdata,
        contentType: "application/json",
        success: callback,
        error: errCallback
    };
    
    log("Requesting " + JSON.stringify(request)); 

    $.ajax(request);
}
Reply all
Reply to author
Forward
0 new messages