18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

how to get Json string from http response

smitty1_2smitty1_2 Members
edited November -1 in Sahi - Open Source
OS: Win7, 64bit
Browser: Firefox 3.6.15
Sahi (Build: 2011-02-13)


Hi,

I'm new to Sahi and to Javascript but I read most of the API doc. I have two different http responses. One is a html site with a table. One is only a JSON String (content type="text/plain") with the same information as in the table.
What I want to do is to see if the results of both reponses are the same. I can access the html easily. But I can't figure out how to get the JSON String in Sahi. I tried to run following code:
<browser>

function javaScriptCode(){

    xmlhttp=new XMLHttpRequest();
        xmlhttp.open("GET", "htt_://localhost:8080/...", true);
        xmlhttp.send();

       if (xmlhttp.readyState==4) {
         var $json = xmlhttp.responseText;
       }

    return $json;
}

</browser>


var $jsonObj;

_set($jsonObj, javaScriptCode());

_alert($jsonObj);

I know there's an underscore. It's a p in the actual code. This will only return me null for the $jsonObj. Is my approach right? Any other suggestions?


Thanks in advance

Comments

  • narayannarayan Administrators
    Hi,

    In the latest version of Sahi (20110314), you could do

    var $jsonObj = _readURL("http://localhost:8080/...";);
    _alert($jsonObj);


    Regards,
    Narayan
  • smitty1_2smitty1_2 Members
    edited March 2011
    Hi,

    thanks for the fast answer! Now I get this error:

    <HTML><HEAD><TITLE>Error</TITLE></HEAD><BODY><H1>Error</H1><HR><!--
    warning: Attempt to write out null variable: error
    --></BODY></HTML>

    But I figured out how to do it. I just put _navigateTo(url); above my javascript call. Then it works fine.


    Best regards


    Edit: I'm sorry, the database was not running. Now I get an error with a template but that's probably a problem with the Servlet. I'll just stick to my solution.
Sign In or Register to comment.