Graham
Graham

Reputation: 8151

Returning control codes as JSON to a jquery ajax json call

I want to know if it is possible to return ascii control codes in JSON format from classic ASP to a jQuery ajax call.

This is my jQuery call:

$.ajax({
    url: "/jsontest.asp",
    type: "POST",
    cache: false,
    dataType: "json",
    complete: function(data)
    {
        var o = $.parseJSON(data.responseText.toString());
    },
    error: function(data1, data2)
    {
        alert("There has been an error - please try again");
    }
});

This is my called page:

{"val1":123,"val2":"abcdef"}

The above works fine, but if I change my called page to include ascii character 31 (1F) like so:

{"val1":123,"val2","abc\x1Fdef"}

then I get the alert in my error function. Can this be done, and if so, how please.

Note: I'm using jQuery 1.7.1 and both IIS 6 and IIS 7

I have tried: \x1f, %1f, and \u001f

Looking at the values in data1 and data2.

data2 has the string "parsererror"

data1 is an object with the following properties:

readyState: 4
setRequestHeader: function (a,b){if(!s){var c=a.toLowerCase();a=m[c]=m[c]||a,l[a]=b}return this}
getAllResponseHeaders: function (){return s===2?n:null}
getResponseHeader: function (a){var c;if(s===2){if(!o){o={};while(c=bH.exec(n))o[c[1].toLowerCase()]=c[2]}c=o[a.toLowerCase()]}return c===b?null:c}
overrideMimeType: function (a){s||(d.mimeType=a);return this}
abort: function (a){a=a||"abort",p&&p.abort(a),w(0,a);return this}
done: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
fail: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
progress: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
state: function (){return e}
isResolved: function (){return!!e}
isRejected: function (){return!!e}
then: function (a,b,c){i.done(a).fail(b).progress(c);return this}
always: function (){i.done.apply(i,arguments).fail.apply(i,arguments);return this}
pipe: function (a,b,c){return f.Deferred(function(d){f.each({done:[a,"resolve"],fail:[b,"reject"],progress:[c,"notify"]},function(a,b){var c=b[0],e=b[1],g;f.isFunction(c)?i[a](function(){g=c.apply(this,arguments),g&&f.isFunction(g.promise)?g.promise().then(d.resolve,d.reject,d.notify):d[e+"With"](this===i?d:this,[g])}):i[a](d[e])})}).promise()}
promise: function (a){if(a==null)a=h;else for(var b in h)a[b]=h[b];return a}
success: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
error: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
complete: function (){if(c){var a=c.length;m(arguments),i?k=c.length:e&&e!==!0&&(j=a,n(e[0],e[1]))}return this}
statusCode: function (a){if(a){var b;if(s<2)for(b in a)j[b]=[j[b],a[b]];else b=a[v.status],v.then(b,b)}return this}
responseText: {"val1":123,"val2":"abcdef"}
status: 200
statusText: OK

Upvotes: 0

Views: 436

Answers (2)

Lian
Lian

Reputation: 2357

Firstly, try using a different ascii code. Character 1F is a "Unit separator". (It's unprintable). Check here.

[Edit: try \x20. It is a space.]

[Edit 2: since you want unit separator and jQuery.parseJSON does not seems to like it, we will use eval().

Now, specifying dataType as 'json' will parse the response from the server; there's no need to manually parse it yourself: We will specific dataType as 'text' and manually parse json with eval():

$.ajax({
    url: "/jsontest.asp",
    type: "POST",
    cache: false,
    dataType: "text",
    success: function(data)
    {
        // data is "{"val1":123,"val2":"abc\x1Fdef"}"
        var o = eval("(" + data + ')'); // parse json

        // will split the text using \x1F as delimiter
        var separated = o.val2.split('\x1F');
        // separated is now ["abc","def"]
    },
    error: function(data)
    {
        alert("There has been an error - please try again");
    }
});

Upvotes: 1

Graham
Graham

Reputation: 8151

I changed the data type of the ajax call to "text" and got a javascript error on the parseJSON call of "bad control character in string literal".

So it seems it can't be done, so I have resorted to some X1F which I replace with ascii 31 when the ajax call returns.

Upvotes: 0

Related Questions