2

I have this code:

    $.ajax({
        type: 'get',
        url: 'https://......./confirma.asp',
        data: $('.newform').serialize(),
        dataType: 'jsonp',
        beforeSend: function () {
            $('.ajax-loader').slideToggle();
        },
        success: function (resposta) {
            alert(resposta); // not work 
            $('.ajax-loader').slideToggle();
        }
    }); 

I get this response from ajax GET (firebug):

CODRET=1&MSGRET=JA CONFIRMADA

How I get this values? For example:

resposta['CODRET'];

or

resposta.CODRET

Because I need set in another JQuery function( );

Patrick Maciel
  • 4,874
  • 8
  • 40
  • 80
  • 1
    `CODRET=1&MSGRET=JA CONFIRMADA` is the **response** you get? This seems to be quite odd, especially as you expect JSONP as response. – Felix Kling Dec 12 '11 at 16:29
  • So you get a query string as a response, which you need to deserialize? – pimvdb Dec 12 '11 at 16:31
  • to use this syntax `resposta['CODRET'];`you need convert response to an object as JSON. – Kakashi Dec 12 '11 at 16:33
  • @Patrick Maciel: Remove a url original antes de postar aqui... – Kakashi Dec 12 '11 at 16:45
  • @FelixKling Yes, this is the "response" and I sent as JSON because otherwise the server doesn't return any value for me. – Patrick Maciel Dec 12 '11 at 17:57
  • @pimvdb I need both values, because from them I will perform other operations with ajax. – Patrick Maciel Dec 12 '11 at 17:59
  • No, you don't send JSON, `$('.newform').serialize()` returns a query string. And `dataType: 'jsonp'` tells jQuery to expect JSONP as response. That's why I asked. It seems `CODRET=1&MSGRET=JA CONFIRMADA` is the data you send. I have never seen that a server returns a query string as response. – Felix Kling Dec 12 '11 at 17:59
  • @Kakashi I don't know how to convert HTML to JSON, anyway, when I try do something after response, nothing work. – Patrick Maciel Dec 12 '11 at 18:02
  • @FelixKling Yes, you are perfectly right. The problem is: I don't know what the server "expects / supports," and the only way I managed to get a return, was using JSON. – Patrick Maciel Dec 12 '11 at 18:06
  • @Patrick Maciel: Check out my answer. – Kakashi Dec 12 '11 at 18:19

3 Answers3

3

Well that looks like a query string. Here is a modified version of this answer:

function parseQueryString(qs) {
    var urlParams = {};
    var e,
        a = /\+/g,  // Regex for replacing addition symbol with a space
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
        q = qs.substring(1);

    while (e = r.exec(q))
       urlParams[d(e[1])] = d(e[2]);
    return urlParams;
}

console.log(parseQueryString("CODRET=1&MSGRET=JA CONFIRMADA"));

http://jsfiddle.net/GphYj/

Community
  • 1
  • 1
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • I tried to use the function, but **nothing works after receiving the response HTML**. I don't know why. – Patrick Maciel Dec 14 '11 at 12:04
  • @PatrickMaciel, were there any errors? You'll need to post what you did – Alex Turpin Dec 14 '11 at 14:21
  • I paste your function in my `php` file, but I cant send "resposta" (response), to function, in other words, you function maybe work **but I cant use that because when I receive a response from server, nothing more work**, its same when you write `'echo'` instead of `'return'` (`echo` breaking the ajax, I think.. it's the same case, you understand me?) - Sorry for my english (_in a moment I put my code as it is._) – Patrick Maciel Dec 19 '11 at 13:46
0

probably you should split them:

$.ajax({
    type: 'get',
    url: 'https://ecommerce.redecard.com.br/pos_virtual/confirma.asp',
    data: $('.newform').serialize(),        
    beforeSend: function () {
        $('.ajax-loader').slideToggle();
    },
    success: function (data) {
        var codret = data.split('&')[0].split('=')[1]; 
        var msgret = data.split('&')[1].split('=')[1];             
        $('.ajax-loader').slideToggle();
    }
});
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Nick Shvelidze
  • 1,564
  • 1
  • 14
  • 28
0

If you can't change the server code do this

  function QueryStringToJsonObject(queryString)
    {
        var queries = {};
        decodeURIComponent(queryString).replace(/([^=]+)=([^&]+)/g,
            function(all, key, value) {
                queries[key.replace(/^&/,'')] = value;
            }
        );
        return queries;
    }

and then:

QueryStringToJsonObject(responseText) ["CODRET"] //1

Otherwise consider returning JSON to http request.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
Kakashi
  • 2,165
  • 14
  • 19