Reputation: 49384
I am posting a form using Jquery Ajax...this is the code:
$( document ).ready(function() {
//callback handler for form submit
$("#myform").submit(function(e)
{
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
$.ajax(
{
url : formURL,
type: "POST",
crossDomain: true,
data : postData,
success:function(data, textStatus, jqXHR)
{
//data: return data from server
alert('it worked');
alert(this.data + "," + this.url);
},
error: function(jqXHR, textStatus, errorThrown)
{
//if fails
alert('it didnt work');
}
});
e.preventDefault(); //STOP default action
e.unbind();
});
$("#myform").submit(); //Submit the FORM
});
This works fine and onsucess I've added this:
alert(this.data + "," + this.url);
And it's giving me the posted data and url.
My problem is that the api that it's submitting to returns data like this after the form is submitted:
<response>
<result>12</result>
<message>Successful.</message>
</response>
What I want to do is to be able to get that response.
How can I get this info?
Upvotes: 2
Views: 22142
Reputation: 722
you can refer bellow example .
$.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data.slice( 0, 100 ) );
}
});
Upvotes: 1
Reputation: 808
You need to replace this.data
with data
, and if you want to access a particular field of the response, add the JSON dataType
:
$.ajax(
{
...
dataType : "json",
success: function(data, textStatus, jqXHR)
{
alert("Result: "+data.result+", Message: "+data.message);
},
...
});
Hope it helps!
Upvotes: 4