1

am using ajax request for calling jersey restful url.

function deRegisterPersonOrganization() {
    var dynamicJson = $('#jsonRequest').val();
    alert("Text Area JSON : " +dynamicJson);
    var jsonObj = {
        "solutionProviderKey" : "e3fad159-ac18-462d-a20e-17763af3689b"
    };
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: rootURL + '/e3fad159-ac18-462d-a20e-17763af3689b/deregister',
        dataType: "json",
        data: JSON.stringify(dynamicJson),
        success: function(data, textStatus, jqXHR){
            alert('Deregister successfull');
        },
        error: function(jqXHR, textStatus, errorThrown){
            alert('Deregister error: ' + textStatus);
        }
    });
}

The problem is i need to give the JSON request where i will give it through the text area. in the above code if i use variable jsonObj in the place of dynamicJson the request is successful. but if i use dynamicJson where in the textarea i give

{
   "solutionProviderKey" : "e3fad159-ac18-462d-a20e-17763af3689b"
}

as request, unable to process request.

Please help me as soon as possible.

Satheesh
  • 646
  • 1
  • 10
  • 33

2 Answers2

2

It does not work because the value from textarea is a string, not a JSON object. I would suggest:

var dynamicJson = eval(textarea.value);

and then pass the dynamicJson into the method call as it is now.

Thai Anh Duc
  • 524
  • 6
  • 12
  • Why is using the JavaScript eval function a bad idea? http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea – Kieran May 21 '13 at 06:43
  • Yes. it is just one suggestion way to turn a string into JSON object. I can see you suggest another way from answer. Thank for the comment. – Thai Anh Duc May 21 '13 at 06:47
  • this doesnt work. the request is changed to form submit event here. – Satheesh May 21 '13 at 06:51
  • But i got an idea that the string should be converted to json from your answer. thanks anyway :) – Satheesh May 21 '13 at 06:53
1

Turn the string into a json object.

var dynamicJson = JSON.toJSON($('#jsonRequest').val());

KJ

Kieran
  • 17,572
  • 7
  • 45
  • 53