Evaluate string in javascript/AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peace2007
    New Member
    • Jul 2007
    • 7

    Evaluate string in javascript/AJAX

    Hi,

    I need to evaluate the return result of a function from Server.aspx as following. But it doesn't work as I need

    [CODE=javascript]function getAjax()
    {
    var XmlHttp;
    //Creating object of XMLHTTP in IE
    try
    {
    XmlHttp = new ActiveXObject(" Msxml2.XMLHTTP" );
    }
    catch(e)
    {
    try
    {
    XmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
    }
    catch(oc)
    {
    XmlHttp = null;
    }
    }
    //Creating object of XMLHTTP in Mozilla and Safari
    if(!XmlHttp && typeof XMLHttpRequest != "undefined" )
    {
    XmlHttp = new XMLHttpRequest( );
    }
    return XmlHttp;
    }
    function GetBanType()
    {
    rnd++;

    url = 'Server.aspx?ac tion=GetBan&ses sion=' + rnd ;
    var re = getAjax();

    re.onreadystate change = function(){
    if( re.readyState == 4 && re.status == 200 )
    {
    if(re.responseT ext != "")
    {
    if (re.responseTex t == "special value")
    {
    ....
    [/CODE]

    Actually, re.responseText returns:
    "Gag

     
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />

    "
    while I'm expecting only "Gag" word.
    Could I know how I can subtract only the first word of the return result? Or, is there any general solution to get a proper result from server .aspx

    Any idea is appreciated.
    Last edited by acoder; Sep 21 '07, 04:18 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Remember to use code tags when posting code:
    &#91;CODE=javas cript]JS code here...[/CODE]

    Your server-side script is displaying extra spaces and newlines. Get rid of them.

    You could replace all spaces and newlines in the response too.

    Comment

    • johnhjohn
      New Member
      • Dec 2006
      • 43

      #3
      If the response from the server is incorrect, then the server must be coded incorrectly in some way. I could not find anything wrong with the javascript code, so check your server.aspx's code to make sure there are not any mistakes that could have caused this error.

      Hope I helped!

      Comment

      • peace2007
        New Member
        • Jul 2007
        • 7

        #4
        Thanks John and acoder for replying!

        I call following function in server.aspx:
        Code:
        private void processGetBan()
                {
                    lock (syncRoot)
                    {
                        string user = (string)Session["username"];
                        string channelname = (string)Session["channel"];
                        Response.Write(Global.Engine.GetChannel(channelname).GetBanType(user));
                    }
                }
        in which GetBanType() returns only "Gag" without any space, etc.
        Is there anything wrong with my server code?
        acoder, you've written that I can replace all spaces and newlines in the response too; could you please tell me how, through a sample? I'm new in javascript/ajax code

        Comment

        • acoder
          Recognized Expert MVP
          • Nov 2006
          • 16032

          #5
          Run the server-side code without Ajax and check the source code.

          To remove spaces/newlines, use the string replace method.

          Comment

          • peace2007
            New Member
            • Jul 2007
            • 7

            #6
            Thank you very much acoder

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              You're welcome.

              Comment

              Working...