2
votes

I have the following code where i construct json data and send to the webservice ,my datatype will be json and response from the webservice will be in xml format does this logic work out or do i need to stick to any one particular datatype either json or xml.

var keyword2 = "{\"keyword1\":\"" + keyword1 + "\",\"streetname\":\"" + address1 + "\",\"lat\":\"" + lat + "\",\"lng\":\"" + lng + "\",\"radius\":\"" + radius + "\"}";

//keyword2 will be my json constructed data ,will it be same in case of xml construction $.ajax({ type: "POST", async: false, url: "/blkseek2/JsonWebService.asmx/GetList", datatype:"json", data:keyword2, contentType: "application/xml; charset=utf-8",

             failure: function(XMLHttpRequest, textStatus, errorThrown) 
                     { ajaxError(XMLHttpRequest,textStatus, errorThrown); },
                success: function(xml) 
                 { ajaxFinish(xml); }

// success: ajaxCallSucceed, // dataType: "xml", // failure: ajaxCallFailed }); });

2

2 Answers

2
votes

See here: http://api.jquery.com/jQuery.ajax/

contentType and dataTypeString are the 2 u need. Like this:

$.ajax(
    {
        type: "POST",
        url: "/prom/" + project + "/Safety/GenerateMapping",
        data: "{\"keyword1\":\"" + keyword1 + "\",\"streetname\":\"" + address1 + "\",\"lat\":\"" + lat + "\",\"lng\":\"" + lng + "\",\"radius\":\"" + radius + "\"}",
        dataType: "json",
        contentType: "xml",
        failure: function(XMLHttpRequest, textStatus, errorThrown) 
                 { ajaxError(XMLHttpRequest,textStatus, errorThrown); },
        success: function(xml) 
             { ajaxFinish(xml); }
     });
1
votes

Yes, just send the JSON object and use eval to get the JSON object using Javascript.

Check this out :

http://www.json.org/js.html