none
Calling Search Web Service in Javascript => Bad Request

    Question

  • Hi all,

     

    I'm tryng to call the Search Web Service of Sharepoint from a Javascript function. Here is the code I'm using:

    function Search()

    {

    var qs = new Querystring();

    var keywordString = qs.get("keywordString");

    var quo;

    quo = String.fromCharCode(34);

    var queryXml = "<QueryPacket xmlns="+quo+"urn:Microsoft.Search.Query"+quo+">";

    queryXml += "<Query domain="+quo+"QDomain"+quo+">";

    queryXml += "<SupportedFormats>";

    queryXml += "<Format revision="+quo+"1"+quo+">urn:Microsoft.Search.Response.Document:Document</Format>";

    queryXml += "</SupportedFormats>";

    queryXml += "<Context>";

    queryXml += "<QueryText language="+quo+"fr-FR"+quo+" type="+quo+"STRING"+quo+">" + keywordString + "</QueryText>";

    queryXml += "</Context>";

    queryXml += "<Range><StartAt>1</StartAt><Count>25</Count></Range>";

    queryXml += "</Query>";

    queryXml += "</QueryPacket>";

    var strEnvelope;

    strEnvelope = "<?xml version="+quo+"1.0"+quo+" encoding="+quo+"utf-8"+quo+"?>";

    strEnvelope += "<soap:Envelope xmlns:xsi="+quo+"http://www.w3.org/2001/XMLSchema-instance"+quo+" xmlns:xsd="+quo+"http://www.w3.org/2001/XMLSchema"+quo+" xmlns:soap="+quo+"http://schemas.xmlsoap.org/soap/envelope/"+quo+">";

    strEnvelope += "<soap:Body>";

    strEnvelope += "<QueryEx xmlns="+quo+"http://microsoft.com/webservices/OfficeServer/QueryService"+quo+">";

    strEnvelope += "<queryXml>"+queryXml+"</queryXml>";

    strEnvelope += "</QueryEx>";

    strEnvelope += "</soap:Body>";

    strEnvelope += "</soap:Envelope>";

    PrepareXmlHttp();

    xmlHttp.open("POST", "http://sharepointemea/sites/vistapoint/_vti_bin/search.asmx?op=QueryEx", true);

    xmlHttp.onreadystatechange = function()

    {

    if (xmlHttp.readyState == 4)

    {

    alert(xmlHttp.statusText);

    var szResponse = xmlHttp.responseText;

    var xmlDoc = new ActiveXObject("Microsoft.XMLDom");

    xmlDoc.async = "false";

    xmlDoc.loadXML(szResponse);

    }

    }

    xmlHttp.setRequestHeader("Content-Type","text/xml; charset=utf-8");

    xmlHttp.setRequestHeader("SOAPAction","http://microsoft.com/webservices/OfficeServer/QueryService/QueryEx");

    xmlHttp.setRequestHeader("Content-Length", strEnvelope.length);

     

    xmlHttp.send(strEnvelope);

    }

     

    But this part of the code: 

    var szResponse = xmlHttp.responseText;

     always displays a "Bad Request" message. I've searched over Internet but found nothing and my code seems to be fine.

     

    Anone got an idea ?

     

    Thanks!

     

     

    Thomas

    Monday, March 12, 2007 10:46 AM

All replies

  • Nobody get an idea ? :(
    Tuesday, March 13, 2007 8:54 AM
  • I too am trying to use the search web service from javascript, my code is below:

     

    function sendSearchSOAP(terms){

    var searchQuery =

    '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlnsTongue Tiedoap="'">http://schemas.xmlsoap.org/soap/envelope/"><soap:Body>' +

    '<QueryPacket xmlns="urn:Microsoft.Search.Query"><Query><Context><QueryText>' + terms + '</QueryText></Context></Query></QueryPacket>'+

        '</soap:Body></soap:Envelope>';

    alert(searchQuery);

    xmlhttp=null;

    // code for Mozilla, etc.
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest()
      }
    // code for IE
    else if (window.ActiveXObject)
      {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
      }
    if (xmlhttp!=null)
      {
      xmlhttp.onreadystatechange=queryStateChange
      xmlhttp.open("POST","http://swisp.scottishwidows.net/_vti_bin/search.asmx",true);
      xmlhttp.setRequestHeader('SOAPAction', 'urn:Microsoft.Search/Query');
      xmlhttp.setRequestHeader("Content-Type", "text/xml");
      xmlhttp.setRequestHeader('charset','utf-8');
      xmlhttp.send(searchQuery);
    }

    }

     

    I am getting a valid SOAP envelope back from the service with an Internal Server Error, is this what you are getting?

     

    I am going mental trying to find out what is wrong. I have managed to return a list of subWebs etcetera with other servces but do not seem to be able to get the search service to work at all.

    Thursday, May 10, 2007 2:44 PM
  • Hi Thomas,

     

    Did you find a solution to this problem. I am having the same problem.

     

     

    Regards,

    Thomas Nielsen

    Tuesday, December 11, 2007 11:05 AM
  • me 2 getting same error.. anybody found the solution?
    Monday, November 18, 2013 1:18 PM