3

I want to use parameters that I receive in an Ajax response, but I'm receiving it as a simple string.

Ajax:

$.ajax({
    type: 'POST',
    url: url,
    data: str
}).done(function(res){
    // res is a simple string
    // => ResponseCode=0&Description=OK
});

Lets say I need to use the Description parameter's value (OK).

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
shmnsw
  • 639
  • 2
  • 11
  • 24
  • Use the `dataType` attribute to receive the response in the desired format. – Mr. Polywhirl Apr 06 '15 at 11:46
  • I may be wrong but will have to use dirty split and dig the value.. it will be better if you can improve the format of your response string.. – Viraj Nalawade Apr 06 '15 at 11:57
  • Duplicate of [How can I get query string values in JavaScript?](http://stackoverflow.com/q/901115/369450). The response you're getting `res` contains URL query parameters. Look at the provided link to find solutions for parsing that. – Uyghur Lives Matter Apr 06 '15 at 13:32

1 Answers1

-3
$.ajax({
    type: 'POST',
    url: url,
    dataType : "json", // define type 
    data: str,
    error : function( er ){ console.log( e );},
    success: function( response ){

    console.log( response );
});
kag
  • 144
  • 1
  • 13
  • this should work an din reality it probably would work for JSON, but use json for your stuff cause it is documented as such. Helps avoid confusion.. I don't have a way to test any of this at now. But good stuff. – Cayce K Apr 06 '15 at 12:36
  • The OP is not using JSON. – Uyghur Lives Matter Apr 06 '15 at 13:35
  • 1
    @KAG The OP's response `ResponseCode=0&Description=OK` is not JSON. This is the format for URL query parameters. Setting *dataType* to `json` will cause an error because the response cannot be parsed as JSON. – Uyghur Lives Matter Apr 06 '15 at 18:50