SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xmlHttp.responseText return empty

    Code:
    function GetXmlHttpObject()
    {
    	var xmlHttp=null;
    	try
    	  {	
    	  // Firefox, Opera 8.0+, Safari
    	  xmlHttp=new XMLHttpRequest();
    	  }
    	catch (e)
    	  {
    	  // Internet Explorer
    	  try
    	    {
    	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	    
    	    }
    	  catch (e)
    	    {
    	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	    }
    	  }
    	return xmlHttp;
    }
    Code:
                    xmlHttp = new GetXmlHttpObject();
    		
    		var url="./scripts/phpAjaxFunction.php";					
    		var params = "action=downloadFileToUnix";		
    	
    		xmlHttp.open("POST", url, true);
    
    		//Send the proper header information along with the request
    		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    		xmlHttp.setRequestHeader("Content-length", params.length);
    		xmlHttp.setRequestHeader("Connection", "close");
    
    		xmlHttp.onreadystatechange = function() {//Call a function when the state changes.
    			if(xmlHttp.readyState == 4 && xmlHttp.status == 200) { 
    			
    				alert(trim(xmlHttp.responseText));
    				
    			}
    		}
    		xmlHttp.send(params);
    xmlHttp.responseText always return empty, can anyone help?
    thanks in advance

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,003
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    The first thing I would do is to check in your browser's console to see if there are any error messages being reported.
    How do I do that?

  3. #3
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,441
    Mentioned
    45 Post(s)
    Tagged
    13 Thread(s)
    Hi Isy,

    Another thing to check - is your server-side script setup to check if the request was made via AJAX? Many JS libraries set the 'x-requested-with' header, so you could try setting this:
    Code JavaScript:
    xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");

  4. #4
    SitePoint Member
    Join Date
    May 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    after adding
    Code:
    xmlHttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xmlHttp.responseText still return empty.

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    What is ./scripts/phpAjaxFunction.php echoing out if you access it directly rather than vis JavaScript?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Member
    Join Date
    May 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by felgall View Post
    What is ./scripts/phpAjaxFunction.php echoing out if you access it directly rather than vis JavaScript?
    what do u means for echoing out?

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,234
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I think what he means is if you go to
    ./scripts/phpAjaxFunction.php
    in your browser, what output do you see? (including both rendered and view-source)

    *hint - AJAX makes an HTTP request as does a browser

  8. #8
    SitePoint Member
    Join Date
    May 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    I think what he means is if you go to
    ./scripts/phpAjaxFunction.php
    in your browser, what output do you see? (including both rendered and view-source)

    *hint - AJAX makes an HTTP request as does a browser
    i can see all the function in ./scripts/phpAjaxFunction.php is executing completely, but still responseText is return empty
    is there anyway to debug it?

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,234
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    I'm happy it's executing completely for you.

    But what is it's output to your browser?

  10. #10
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,003
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by lsy View Post
    is there anyway to debug it?
    Aside from the many good suggestions in this thread already, you could do the following (assumes you are using the Chrome browser):

    Open the website where you want to inspect request and response.
    Click F12.
    Select the Network tab.
    Here you will be able to see all requests and responses and thus pinpoint your problem.

  11. #11
    SitePoint Member
    Join Date
    May 2010
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it suppose to return some text in the echo, it return in my old server, after i move to new server, it is not.
    i use the debugger, no error found.

  12. #12
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,234
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)
    Might it have something to do with same origin policy?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •