Reputation: 67
How can I use ajax to post value from user input to my web service api?
It seems that I can't append value at ajax url.
The following is my code...
<form action="javascript:Start()">
<input type="text" id="target" value="tar"/>
<input type="text" id="profile" value="pro"/>
<input type="submit" value="Submit">
</form>
function Start()
{
var target = $('#target').val();
var profile = $('#profile').val();
if(validateIdata())
{
$.ajax({
type: 'POST',
url: "webapi/"+ target + "/" + profile,
data: $form.serialize(),
success: function(){
alert('success');
}
});
}
return false;
}
Thanks
Upvotes: 2
Views: 86
Reputation: 2604
You can solve it passing data on URL instead of data field. Try this.
Html. (add id
to form in order to access later to its data)
<form id="form" action="javascript:Start()">
<input type="text" id="target" value="tar"/>
<input type="text" id="profile" value="pro"/>
<input type="submit" value="Submit">
</form>
Javascript.
function Start()
{
var target = $('#target').val();
var profile = $('#profile').val();
if(validateIdata())
{
$.ajax({
type: 'POST',
url: "webapi/"+ target + "/" + profile?+$('#form').serialize(),
success: function(){
alert('success');
}
});
}
return false;
}
Upvotes: -1
Reputation: 943100
You are serialising all the successful form controls, but your form doesn't have any controls that can be successful. They need to have name
attributes.
This assumes that you have defined $form
somewhere, you haven't in the code you have shared, if you haven't then you need to do that first.
Upvotes: 2