2

I am trying to send JSON string

var json = {"city":value1, "country":value2};
$.ajax({
    url : url,
    data : json,
    dataType : 'json',
    success : function(response) {
        alert(response);
    }
})

In the URL to which I make ajax call I am not getting how to get this string value there? What should I use request.getParameter? What should be the value in the parameter?

4
  • 1
    You aren't actually sending json. Commented Apr 5, 2013 at 15:47
  • You've missed a bracket, I've added it for you (after alert(response)) Commented Apr 5, 2013 at 15:52
  • @MMM:Yes. I mistyped it.. Thanks for editing. Commented Apr 5, 2013 at 15:54
  • @Musa: NO I am not sending actual json I have to send a lot of data thought of this way not sure if any better way exist but that would be a total different question. I can convert the string to JSON object in the serversided JSP page. Commented Apr 5, 2013 at 15:55

3 Answers 3

3

Ajax request :

 var jsonObj= { jsonObj: [... your elements ...]};

    $.ajax({
        type: 'post',
        url: 'Your-URI',
        data: JSON.stringify(jsonObj),
        contentType: "application/json; charset=utf-8",
        traditional: true,
        success: function (data) {
            ...
        }
    });

on server side :

String city =    request.getParameter("city");

String country=    request.getParameter("country");
Sign up to request clarification or add additional context in comments.

6 Comments

Actually what have you seen in firebug ??what are the values going from client side ??have you seen in firebug ?
Basically I have not idea how to send this parameter to the server side JSP page. I am not getting correct value in the JSP page hence resulting in incorrect output form the JSP page I get the error message form the AJAX call, which I have omitted in this question.
So ,please follow this question stackoverflow.com/questions/9822361/…
look at the @ioseb answer in the link and then write my lines in server code ..this is the best stackoverflow.com/questions/8184438/…
Thanks for helping me +1 for giving nice comment form which I got an Idea for solving the problem. I have posted my solution. Please have a look. I am not sure it is the best way. I could use your feed back. Thanks again.
|
2

This might be a bad idea but has done the job. Thanks everyone for sharing your thoughts had hard time sending data. I saw all the suggested answers by @baadshah but, I couldn't implement a single one. :(. I reanalyzed the problem.

My problem is I can't retrieve the JSON data in the server side page where as I was able to access other elements. My HTML page had one of these

  <input type = "text" name = "fname" class = "imp"/>

In my JSP page I could use

 String fname = request.getParameter("fname");

After being stuck for more than couple of hours and getting frustrated I thought for another way. This is the solution I found. This problem would be solved if I can club the JSON string with any input tag with a valid name. The next moment I added this line in script tag

$('input[name=hide]').val(json);
var dataToBeSent = $("form#hidden").serialize();

In the HTML part I added following snippet.

<form name="hidden" id="hidden">
     <input type="hidden" name="hide"/>
</form>

This solved my problem. This might not be the best way around but it did the job.

1 Comment

Actually hard to get the problem and your counter also looks promising..cheers :) +1.
0

type: "POST"

should do the trick.

4 Comments

What should I use in request.getParameter()?
@Dibya check with my answer.Revert back if any problem.
@MMM Super liked the responsibility of edit without leaving with comment.But Op is asking about the retrieving the parameters on server side ??? Am i misunderstood ?
@Baadshah: No you got my point. I am asking to retrieve parameter on the server side.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.