views:

101

answers:

1

The following code inside the tags gives error: "Object Expected".

    <!-- JQuery/AJAX-->
    <script type="text/javascript">
        try {
            $(document).ready(function(){
                $("p").load(function(){
                    MakeRequest('divElectionCategory','ulElectionCategory','SELECT * FROM electioncategorymaster', 'UnOrderedList');
                });
            });
        }
        catch(e)
        {
            alert(e.message);
        }
    </script>

The MakeRequest function resides in a separate .js file and I have included that file before the above given code.

Which object it is referring to?

Edited: The MakeRequest function

function MakeRequest(DivName, ControlName, SqlQuery, ControlType)
{
  var xmlHttp = getXMLHttp();
  var strUrl = "";

  if (ControlType = 'DropDown')
      strUrl = "../phplibraries/filldropdown.php?DivName=" + DivName + "&DropDownControlName=" + ControlName + "&SqlQuery=" + SqlQuery;
  else
      strUrl = "../phplibraries/createelectioncategorymenu.php?DivName=" + DivName + "&ulName=" + ControlName + "&SqlQuery=" + SqlQuery;

  alert(strUrl);
  try 
  {
    xmlHttp.onreadystatechange = function()
    {
        if (xmlHttp.readyState == 4) 
        {
            HandleResponse(xmlHttp.responseText, DivName);
        }
    }
            xmlHttp.open("GET", strUrl, true);
            xmlHttp.send(null);
    }
    catch(err)
    {
        alert(err);
    }
}

I know there is a big security issue above but please ignore it at this point of time.

+1  A: 

You cannot call load() that way.

The first parameter of load takes a URL not a function. Perhaps you meant this:

$("p").load( MakeRequest('divElectionCategory','ulElectionCategory','SELECT * FROM electioncategorymaster', 'UnOrderedList') );

That assumes that MakeRequest returns a formatted URL.

EDIT

.load() when used against a DOM element and the first parameter is a function, jQuery assumes you are attaching an event handler. However, p does not have a load event. If you want to wait for everything to load, try this (It doesn't have to be in DOM ready):

$(window).load( function(){
   MakeRequest('divElectionCategory','ulElectionCategory','SELECT * FROM electioncategorymaster', 'UnOrderedList')
});

MakeRequest rewrite

function MakeRequest(DivName, ControlName, SqlQuery, ControlType)
{
  var strUrl = "", params = {};

  if (ControlType = 'DropDown'){
    strUrl = "../phplibraries/filldropdown.php"; 
    params = {
      DivName: DivName,
      DropDownControlName: ControlName,
      SqlQuery: SqlQuery
    }
  } else {
    strUrl = "../phplibraries/createelectioncategorymenu.php";  
    params = {
      DivName: DivName,
      ulName: ControlName,
      SqlQuery: SqlQuery
    }
  }


  alert(strUrl);
  $.get(strUrl, params, function(data){
    HandleResponse(data, DivName);
  });
}
Doug Neiner
Oh Doug, its you. Yes it worked but the .js file that contains the MakeRequest function also contains an alert() in the MakeRequest. It is not displayed. I guess it is not reaching MakeRequest.
RPK
Can you post what the MakeRequest looks like? Just to be sure are you thinking `load` is an event on the `p` element? Or are you trying to make an AJAX request?
Doug Neiner
Oh no, it is giving error again.
RPK
Ok, editing above.
RPK
RPK, why not use jQuery's AJAX methods?
Jonathan Sampson
Can you please illustrate?
RPK
@Doug: I did like that before.
RPK
I rewrote the MakeRequest function using jQuery AJAX methods.
Doug Neiner
Trying it and come back to you after some time.
RPK
@Doug: Still getting same error: "Object Expected".
RPK
@RPK try removing things one by one until the error goes away. For instance remove just the `HandleResponse` call, but leave the rest of that anonymous function alone. There is a lot of nested functions, so its hard to troubleshoot.
Doug Neiner
@Doug: If I put alert(), just above HandleResponse, it is coming there.
RPK
Hey man, I am done for tonight. Ok, maybe the error is in HandleResponse. Can you post or post via http://pastie.org and share the link?
Doug Neiner