-1

i'm trying to get the values submitted by my Ajax call in my jsp using the request.getParameter(); but it keeps giving null, This is my $.Ajax

function get(){ 
var select = document.getElementById("model");
var selectedString = select.options[select.selectedIndex].value;
alert(selectedString);
$.ajax({
    type:"POST",
    url:"index.jsp",
    data:{fram:selectedString},
    dataType:"text",
    success:function(data){
    alert("data loaded: " );
}});
}

this is what triggers the get();

<select id="model" class="form-control"  name="From"style="background:#FCDFD5;" onchange="get()">
<% 
while(r.next()){
%>
<option><%=r.getString(3)%></option>
<% } %></select>

the alert works but it says null when i try to print out the value in my jsp, This is my request.getParameter();

<% String name=request.getParameter("fram");
    out.println(name);
%>

Thanks a lot in advance

Hiro_Hamada
  • 149
  • 2
  • 12
King ja
  • 51
  • 5
  • 1
    How can `alert("data loaded successfully" );` ever say `null` ? – adeneo Dec 18 '15 at 20:05
  • 1
    What is the value of selectedString? – Robbert Dec 18 '15 at 20:05
  • selectedString has to have a value given to it in the javascript. please update your post showing how you set this variable and where it is defined in the javascript – Nikki9696 Dec 18 '15 at 20:25
  • selectedString is the selected value in a dropdown list – King ja Dec 18 '15 at 20:25
  • unless it's set as a global variable or in the same scope as the ajax call, it's null in the script you posted here – Nikki9696 Dec 18 '15 at 20:25
  • also, you can run your code in chrome and set a breakpoint there to verify the value of the variable prior to sending it – Nikki9696 Dec 18 '15 at 20:27
  • i have updated my question – King ja Dec 18 '15 at 20:31
  • your options have no values? – Nikki9696 Dec 18 '15 at 20:35
  • try this, see if it helps – Nikki9696 Dec 18 '15 at 20:35
  • it is getting the selected value in the dropdown, i want to submit that selected value to my jsp page, get that value and use for another query, but it is not getting the value in the page i submitted it to, it would alert the value on that page, but when i try to print it out, it says null – King ja Dec 18 '15 at 20:40
  • hmmm, have you checked with chrome (or fiddler) to verify the actual ajax call is passing what you think it's passing? Ajax sends data in the body, and I'm not positive that JSP will look for data there using request parameters. Kinda a guess at this point. – Nikki9696 Dec 18 '15 at 20:43
  • oh look, this might be the same issue - posting JSON has issues http://stackoverflow.com/questions/19431817/string-string-request-getparameterdata-is-null – Nikki9696 Dec 18 '15 at 20:44

1 Answers1

-1

I'm going to leave this one in an answer: seems like the same problem as this thread. Ajax is not submitting a form, it's sending a JSON request. Request.getParameter expects form POST or url params, the JSON submitted as body content from ajax confuses it, even if it's just a string and not a complex type. It's not where getParameter is looking for it.

Community
  • 1
  • 1
Nikki9696
  • 6,260
  • 1
  • 28
  • 23
  • i'm sorry but i am new to this, so what i have to do is find a way to convert the JSON request to url parameters – King ja Dec 18 '15 at 20:57
  • well, I don't know what your jsp looks like, but you can try making it submit the ajax like a form kinda like this http://stackoverflow.com/questions/30150148/request-getparameter-returns-null-when-using-ajax-with-json-object-as-data – Nikki9696 Dec 18 '15 at 21:20