java - JQuery Ajax Requests behaving unexpectedly -
i using jquerys ajax method talk web service. code seems ok, monitored http traffic using httpfox firefox plugin , noticed unexpected results. begin with, setting contenttype
application/json
, web service producing json
data httpfox indicates content type http requests application/vnd.sun.wadl+xml (ns_error_dom_bad_uri)
.
the request method get
set in ajax request, httpfox indicates request method options
. , while request succeeds , data returned, onsuccess
method of ajax request not called. instead, onerror
method called. http fox able capture data web service response. see image http fox.
finally, other request other processes in browser seem ok http requests flagged 'red' http fox. request other pages , processes seem ok.( green or white).
i have attached screenshot of httpfox highlighted on 1 of request. flagged ones application.
image:
i have pasted ajax code using make http requests.
window.onload = function() { var seq_no = getparameterbyname("seq_no"); var mileage = getparameterbyname("mileage"); document.getelementbyid("seq_no").value = seq_no; document.getelementbyid("mileage").value = mileage; var param = 'vehreg='+encodeuricomponent(document.getelementbyid('vehreg').value); // alert(param); loadvehicleinfo(param); }; function loadvehicleinfo(params) { $("#message").html('<p><font color="green">loading...</font></p>'); $.ajax({ type: "get", url: "http://localhost:8080/stockcloud/rest/vehicles/info", data: params, contenttype: "application/json; charset=utf-8", datatype: "json", success: function(data,status) { $("#message").empty(); $("#message").html('<p>'+getasuriparameters(data)+'</p>'); }, error : function(xmlhttprequest, textstatus, errorthrown) { $("#message").html("<p> <font color='red'>the following error occurred: " +textstatus+ ': '+errorthrown+ "</font>"); } }); }; function getasuriparameters (data) { return object.keys(data).map(function (k) { if (_.isarray(data[k])) { var keye = encodeuricomponent(k + '[]'); return data[k].map(function (subdata) { return keye + '=' + encodeuricomponent(subdata); }).join('&'); } else { return encodeuricomponent(k) + '=' + encodeuricomponent(data[k]); } }).join('&'); }; function getparameterbyname(name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new regexp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(location.search); return results == null ? "" : decodeuricomponent(results[1].replace(/\+/g, " ")); }
server side code request:
@path("/vehicles")
public class vehiclesservice {
@get @path("info") @produces("application/json") public response getvehicleinfo(@defaultvalue("__default__") @queryparam("vehreg") string vehreg) { // send soap message soap server serverresponse resp = new serverresponse(); if("__default__".equals(vehreg)) { resp.seterror("vehicle registration must supplied query parameter: ?vehreg=<the reg no>"); resp.setresult(false); response.status(response.status.bad_request).entity(resp).build(); } try { // actual code return car info , return xml string info. connection.disconnect(); string xml = urldecoder.decode(s.tostring(),"utf-8"); xml = xml.replace("<", "<").replace(">", ">").replace("<?xml version='1.0' standalone='yes' ?>", ""); system.out.println(xml); resp.setvehicle(new vehicleparse().parse(xml)); resp.setresult(true); } catch(exception e) { resp.setresult(false); resp.seterror(e.getmessage()); e.printstacktrace(); response.status(response.status.internal_server_error).entity(resp).build(); } return response.status(response.status.ok).entity(resp).build(); } }
is there not doing right?
thanks.
Comments
Post a Comment