I am puzzled, I have a call with jQuery using .load to fill a DIV with content composed of transformed XML into HTML, but whenever I call load (or $.get or $.ajax for that matter) I get a “400 Bad Request – The request sent by the client was syntactically incorrect” message.
The parameters are all there and even when I set up a test call for a page with no parameters I get the same message. All this is being done via Spring (v3) with an annotated controller. What’s confusing is that I already have a portion of this working with calls to a web service that return a JSON object to fill a SELECT and that works fine.
How do I fill the DIV with HTML?
Javascript jQuery call:
function doSearch()
{
var sUrl = "query.html";
var sSecurityToken = '?st=' + $('#ihSecurityToken').val();
var sProjects = '';
$("#sltProjects option:selected").each(function ()
{
sProjects += $(this).val() + ';';
});
sProjects = '&pn=' + escape(sProjects);
var sDatabases = '';
$("#sltDatabases option:selected").each(function ()
{
sDatabases += $(this).val() + ';';
});
sDatabases = '&db=' + escape(sDatabases);
var sQueryText = '&qt=' + escape($('#taSearchText').val());
sSort = '&sr=' + $('#sltSort').val();
sResultsPerPage = '&rp=' + $('#sltResultsPerPage').val();
sRelevance = '&rl=' + $('#sltRelevance').val();
var sFilters = '&ft=';
var sTemp = '';
$("[id^=fld]").each( function()
{
sName = $(this).attr('alt');
sValue = escape($(this).val());
sTemp += escape((sName + '=' + sValue + ';'));
});
if (sTemp.length)
{
sFilters += sTemp;
}
else
{
sFilters = '';
}
var sStartingPage = "&sp=1";
sFinal = sUrl + sSecurityToken + sProjects + sDatabases + sQueryText + sSort + sResultsPerPage + sRelevance + sFilters + sStartingPage;
$('#resultsBlock').load(sFinal);
}
Controller code:
package enterprisesearch.web;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.xml.sax.SAXException;
import enterprisesearch.domain.DatabaseFilter;
import enterprisesearch.domain.UserDatabaseProject;
import enterprisesearch.domain.service.DouglasService;
/**
* @author bob
*
*/
@Controller
public class SearchController
{
protected final Log logger = LogFactory.getLog(getClass());
private DouglasService dgWebService = new DouglasService("http://localhost:8080/Douglas/services/Douglas");
@RequestMapping("/search.html")
@ModelAttribute("message")
public String getMessage()
{
return "Results go here...";
}
@ModelAttribute("username")
public String getUsername(@RequestParam(value="username", required=true) String psUsername)
{
return psUsername;
}
@ModelAttribute("securitytoken")
public String getSecurityToken(@RequestParam(value="username", required=true) String psUsername)
{
String sReturn = "";
Map<String, String> mpResult = this.dgWebService.getSecurityToken(psUsername);
if (mpResult.containsValue("SUCCESS"))
{
sReturn = mpResult.get("securityinfo");
}
else
{
sReturn = mpResult.get("errorid");
}
return sReturn;
}
@ModelAttribute("userprojects")
public Collection<UserDatabaseProject> getUserProjects(@RequestParam(value="username", required=true) String psUsername) throws ParserConfigurationException, SAXException, IOException
{
String sUsername = "";
if (psUsername == null || psUsername.isEmpty())
{
sUsername = "lymana";
}
else
{
sUsername = psUsername;
}
return this.dgWebService.getUserDatabaseProjects(sUsername);
}
@RequestMapping("/databases.html")
public @ResponseBody Map<String, String> getUserDatabases(@RequestParam(value="username", required=true) String psUsername, @RequestParam(value="projectname", required=true) String psProjectName) throws ParserConfigurationException, SAXException, IOException
{
Map<String, String> mpDatabases = new HashMap<String, String>();
Collection<UserDatabaseProject> udpProjects = this.dgWebService.getUserDatabaseProjects(psUsername);
if (udpProjects.size() > 0)
{
for (UserDatabaseProject udpProject : udpProjects)
{
if (psProjectName.indexOf(udpProject.get_msProjectName()) != -1)
{
mpDatabases.put(udpProject.get_msDatabaseName(), udpProject.get_msDatabaseDescription());
}
}
}
else
{
mpDatabases.put("","No databases were found for the selected project");
}
return mpDatabases;
}
@RequestMapping("/filters.html")
public @ResponseBody Map<String, String> getCommonDBFilters(@RequestParam(value="databases", required=true) String psDatabaseNames) throws ParserConfigurationException, SAXException, IOException
{
Map<String, String> mpDatabases = new HashMap<String, String>();
Collection<DatabaseFilter> dfFilters = this.dgWebService.getCommonDBFilters(psDatabaseNames);
if (dfFilters.size() > 0)
{
for (DatabaseFilter dfFilter : dfFilters)
{
if (psDatabaseNames.indexOf(dfFilter.get_msDatabaseName()) != -1)
{
mpDatabases.put(dfFilter.get_msFilterName(), dfFilter.get_msFilterName());
}
}
}
else
{
mpDatabases.put("","No filters were found for the selected database(s)");
}
return mpDatabases;
}
@RequestMapping("/test.html")
public ModelAndView execTest()
{
//Map<String, String> mpDatabases = new HashMap<String, String>();
ModelAndView mavReturn = new ModelAndView("test.jsp");
//mpDatabases.put("test","<b>No filters were found for the selected database(s)</b>");
return mavReturn;
}
//@ModelAttribute("searchResults")
/* @RequestMapping(value = "/query.html", method = RequestMethod.GET)
public ModelAndView doSearch(@RequestParam(value="st", required=true) String psSecurityToken,
@RequestParam(value="pn", required=true) String psProjects,
@RequestParam(value="db", required=true) String psDatabases,
@RequestParam(value="qt", required=true) String psQueryText,
@RequestParam(value="sr", required=true) String psSort,
@RequestParam(value="rp", required=true) String psResultsPerPage,
@RequestParam(value="rl", required=true) String psRelevance,
@RequestParam(value="sp", required=true) String psStartingPage,
@RequestParam(value="ft", required=false) String psFilters)
{
ModelAndView mavReturn = new ModelAndView("query.jsp");
String sTemp = "";
String sOptions = "";
String sStartingPage = "1";
if (!psStartingPage.isEmpty())
{
sStartingPage = psStartingPage;
}
String sSort = "Sort=" + psSort;
String sResultsPerPage = "ResultsStart=" + sStartingPage + ";ResultsEnd=" + psResultsPerPage;
String sDatabases = "Repositories=" + psDatabases.replace(';', ',');
String sRelevance = "RelevanceMin=" + psRelevance;
String sDefaults = "Print=All;SummaryType=Context;Highlighting=Terms+SummaryTerms;";
sOptions = sSort + ";" + sResultsPerPage + ";" + sDatabases + ";" + sRelevance + ";" + sDefaults;
//sReturn = this.dgWebService.executeTextQuery(psSecurityToken, psProjects, psDatabases, psQueryText, sOptions, psFilters);
sTemp = "<b>This is a test</b>";
mavReturn.addObject("searchResults", sTemp);
return mavReturn;
}*/
/*@RequestMapping(value = "/test.html", method = RequestMethod.GET)
public ModelAndView doTest(@RequestParam(value="st", required=true) String psSecurityToken)
{
String sTemp = "";
ModelAndView mavReturn = new ModelAndView("test.jsp");
//sReturn = this.dgWebService.executeTextQuery(psSecurityToken, psProjects, psDatabases, psQueryText, sOptions, psFilters);
sTemp = "<b>This is a test</b>";
mavReturn.addObject("searchResults", sTemp);
return mavReturn;
}*/
}
URL:
http://localhost:8085/EnterpriseSearch/query.html?st=kjahsdf9845hasfha9348uwhefas&pn=ECC%3B&db=dbECC%3B&qt=money&sr=relevance&rp=10&rl=90&sp=1&ft=false
Firebug info:
Response Headersview source
Server Apache-Coyote/1.1
Content-Type text/html;charset=utf-8
Content-Length 971
Date Thu, 03 Nov 2011 20:06:56 GMT
Connection close
Request Headersview source
Host localhost:8085
User-Agent Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23
Accept text/html, */*; q=0.01
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 115
Connection keep-alive
X-Requested-With XMLHttpRequest
Referer http://localhost:8085/EnterpriseSearch/search.html?username=lymana
Cookie JSESSIONID=CC30B029E8C9215489D4F938A6C1BB9D
Any ideas? What am I missing?
Updated to include the complete Controller code.
I copied and pasted your Java code into my Spring 3.0 testing application, and hit that URL manually. If you hit that URL directly, do you get the same 400?
There are a few simple problems I could see happening, but you’ve most likely already ruled them out.. I just don’t have enough info reading your message to know!