0

I have multiple values, I am trying pass all values in url to controller class at one time.

But last value passed in url.

function submitFormData(formData) {
var x = []; 
for(var i = 0;i < formData.length ;i++ ){
    alert(i);
    x = [];
    x.push(formData[i].name);
    x.push(formData[i].email);
    x.push(formData[i].message);
}
 var url= '/userrecords?x='+x;
alert(url);
     $.ajax({
            type: 'POST',
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            beforeSend: beforeSendHandler,
            url: url,
            success: function(result){
            if(result.success == true) {
              $('.alert-success').show();
              $('.alert-danger').hide();
              $("#successmsg").html(result.msg);
              setTimeout(function() {
                $(".alert-success").alert('close');
              }, 10000);
            } else {
              $('.alert-danger').show();
              $('.alert-success').hide();
              $("#error").html(result.msg);
              setTimeout(function() {
                $(".alert-danger").alert('close');
              }, 10000);
            }
            }
    });
}

controller class

@RequestMapping(value = "/userrecords")
public @ResponseBody StatusResponse saveList(@RequestParam(required = false) String x,Model model)
    throws ParseException, SQLIntegrityConstraintViolationException {
    //read all values here
} 

What is wrong in my code. And how to read all values in controller.

6
  • Why are you sending data in URL, if you are doing a 'POST' ? Commented Mar 28, 2017 at 5:47
  • can you please share the sample formDat and the url Commented Mar 28, 2017 at 5:56
  • You dont need 'x' variable to post data, formdata is enough to post your data. In controller you can access your post data by name e.g. 'name', 'message' etc. Commented Mar 28, 2017 at 5:57
  • @MUT: How can access data. I am getting null values. My class name is 'UserData' Commented Mar 28, 2017 at 6:02
  • Trying using binding your data to model like , public saveList([Bind(Include = "name,email,message")] Model model' ) { // save to db db.Add(model)} It did work in .net mvc. Commented Mar 28, 2017 at 6:11

2 Answers 2

1

Convert your array output in JSON and send it to using AJAX and also you have to define content type is JSON.

you can also use jquery ajax it is very simple for request response.

$.ajax({
type: "POST",
dataType: 'json',
url:"URL here",
success: function(data) // response 
{}
});
Sign up to request clarification or add additional context in comments.

1 Comment

please look at my updated question. i have class so how to get all values in controller?
0

I think you should post your formdata as ajax data as below.

Pass your x veriable as a ajax data.

function submitFormData(formData) {
var x = []; 
for(var i = 0;i < formData.length ;i++ ){
    alert(i);
    x.push(formData[i].name);
    x.push(formData[i].email);
    x.push(formData[i].message);
}
 var url= '/userrecords';
alert(url);
     $.ajax({
            type: 'POST',
            data: x,
            cache: false,
            processData: false,
            contentType: false,
            beforeSend: beforeSendHandler,
            url: url,
            success: function(result){
            if(result.success == true) {
              $('.alert-success').show();
              $('.alert-danger').hide();
              $("#successmsg").html(result.msg);
              setTimeout(function() {
                $(".alert-success").alert('close');
              }, 10000);
            } else {
              $('.alert-danger').show();
              $('.alert-success').hide();
              $("#error").html(result.msg);
              setTimeout(function() {
                $(".alert-danger").alert('close');
              }, 10000);
            }
            }
          });
}

4 Comments

Okay,but how can access data in controller class?
follow this link danylkoweb.com/Blog/…
i am using java
Than sorry because i am not familiar with java.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.