Hi, I am new to JSON .My question is how to pass JSON data to restful web services through ajax?
Please Help me.
I tried by following code but I am not sure about it
MY INDEX PAGE
<script type="text/javascript">
$(document).ready(function(){
var uname = document.getElementById("uname").value();
var password = document.getElementById("pwd").value();
$('#ok').click(function(){
$.ajax({
url:'http://localhost:8090/LoginAuthRWS/rest/orders',
type:'post',
dataType: 'Jsondemo',
success: function(data) {
$('#name').val(data.name);
$('#email').val(data.email);
var JSONObject= {
"uname":uname,
"password":password
};
}
});
});
});
</script>
var JSONObject= {"uname":uname, "password":password };
var jsonData = JSON.parse( JSONObject );
var request = $.ajax({
url: "rest/orders",
type: "POST",
data: jsonData,
dataType: "json"
});
Problems with your code:
.value
is a property not an functiondata
of $.ajax
Jsondemo
you have to use JSON
data
is not JSON you can use $.parseJSON
to convertit to JSONComplete Code
$(document).ready(function(){
$('#ok').click(function(){
var uname = document.getElementById("uname").value;
var password = document.getElementById("pwd").value;
var JSONObject= {
"uname":uname,
"password":password
};
$.ajax({
url:'http://localhost:8090/LoginAuthRWS/rest/orders',
type:'post',
data : JSONObject,
dataType: 'JSON',
success: function(data) {
var jsonData = $.parseJSON(data); //if data is not json
$('#name').val(jsonData.name);
$('#email').val(jsonData.email);
}
});
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With