0

I am trying to pass 12mca22@nirmauni.ac.in in ajax but it is giving me such error

'unexpected Token Illegal.'

how can I pass such a string in ajax?

this is My Code

$.ajax({
            url:'http://localhost/student',
            type: 'POST',
            data: {
                data:"12mca22@nirmauni.ac.in"
            },
            success: function(data) {
                alert(data);
            }
        });
Bhavik Joshi
  • 2,557
  • 6
  • 24
  • 48

1 Answers1

0

Your string is perfectly fine, but jQuery's just throwing a fit because you've got an extra comma. Remove it:

data: {
    data:"12mca22@nirmauni.ac.in"
},

and you should be fine. jQuery expects the data field to contain a JSON object, so that extra comma there makes the JSON syntax invalid and it's unexpected, like the error message says

The below code executes fine.

$.ajax({
    url:'http://httpbin.org/post',
    type: 'POST',
    data: {
        data:"12mca22@nirmauni.ac.in"
    },
    success: function(data) {
        console.log(data);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

enter image description here

JSFiddle

enter image description here

Huey
  • 5,110
  • 6
  • 32
  • 44