3
votes

I have deployed the adapter on Worklight server and there is some requirement where I am calling worklight adapter from outside as a rest serverice , it is working fine and returning data as required but instead of giving json output it is giving HTML

<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>Invoke Procedure Result</title><script src="/secure/console/js/jquery-1.6.min.js"></script><style>   textarea {       width: 100%;   }   .textwrapper {       margin: 5px 0;       padding: 3px;   }</style></head><body onload="attachEvent();"><div><span id="invRes">Invocation Result of procedure: 'Authentication' from the Worklight Server</span>: </div><div id="target"><textarea rows="20">{
   "RESPONSE": {

      "USER_ID": "292265"
   },
   "errors": [
   ],
   "info": [
   ],
   "isSuccessful": true,
   "responseHeaders": {
      "Content-Length": "1195",
      "Content-Type": "text\/xml;charset=ISO-8859-1",
      "Date": "Thu, 21 Nov 2013 10:10:13 GMT",
      "Server": "Oracle GlassFish Server 3.1.2.2",
      "X-Powered-By": "Servlet\/3.0 JSP\/2.2 (Oracle GlassFish Server 3.1.2.2 Java\/Oracle Corporation\/1.7)"
   },
   "responseTime": 4234,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 4235,
   "warnings": [
   ]
}</textarea></div><script>function attachEvent() {$('#target').ajaxError(function(e, xhr, ajaxOptions, thrownError){$(this).text("Error: Please ensure that the XML input and XSL transformation are valid and try again.");});}function run_xslt() {var xml = $('#originalXML').val();var xsl = $('#originalXSL').val();$.post('/secure/dev/xslt',{'xml':xml,'xsl':xsl},function(data, textStatus, XMLHttpRequest){$('#target').empty();json = $("<textarea></textarea>");json.attr("rows",25);json.text(data);$('#target').append(json);$('#invRes').text('Result of Local XSL Transformation');},'text');}</script></body></html>

in code I am again parsing it from HTML and storing json into string. Then only i can use it. This is the url given below for calling adapter externally as per worklight docs.

http://WorklightServer.com/secure/dev/invoke?adapter=Reports&procedure=Authentication&parameters=%5B%5D

2
Can you provide more information on what you have implemented in your adapter procedure and what you are returning from itSrik
i have implemented adapter on worklight 6 version and return type is xml in adapter. If i run adapter from worklight server by invoke procedure method it is giving json output but i access from url it is returning html which contain json instead of json as provided above.vishal_g
@vishal_g Can you please provide the worklight doc link, that describes this topic?Mohammad Ashfaq

2 Answers

4
votes

remove /dev/ component from the URL, it is for development ease purposes only. without it you'll get your JSON.

1
votes

I had the same issue and after reading Anton's answers I set the "dataType" of the Ajax call to "text" and then edit the response to remove the /*-secure- and */ and then parsed the string to get the JSON "JSON.parse(theString)"

$.ajax({
            type: 'POST',
            url: ajaxURL,               
            async: true,
            cache: true,
            timeout: 5,
            dataType: "text",
            success: function(data){                    
                data = data.replace("/*-secure-","");
                data = data.replace("*/","");                   
                var dataJSON = JSON.parse(data);                    
                //Do success                
            },
            error: function(data, statusCode){
                //Do error                  
            }
        });