AJAX Post - Performed in JSP, need to return java based variables in the AJAX POST Success function
Date : March 29 2020, 07:55 AM
seems to work fine If you know the structure of the data that's coming in (you should!), create an object that the post data can be serialized to (I'm assuming myData is json?... if not, it should be!) by the servlet. The spring framework provides the @RequestBody annotation to deserialize the incoming json to your object. When the servlet needs to respond, do what @Jigar recommended: wrap your response in an object. The spring framework provides the @ResponseBody annotation to serialize your response to json. It could look something like this: Your js: var myData = { postId: 1, comment: "this is great!" };
$.ajax({
type: "POST",
url: url,
dataType: "text", // [text, xml, json, script, text, html]
data: {postData : myData, sendURL : postUrl},
success: function(data, textStatus, jqXHR) {
var jsonRepsonse = JSON.parse(data);
alert('Success post to URL entered \n\n The data returned the following: ' + jsonRepsonse.responseText + ", " + jsonRepsonse.responseCode);
},
error:function (xhr, ajaxOptions, thrownError){
alert('Error xhr : ' + xhr.status);
alert('Error thrown error: ' + thrownError);
}
//complete: alert('complete')
});
class Comment {
private long postId;
private String comment;
// getters & setters
}
class AjaxResponse{
private String responseText;
private String responseCode;
//other stuff
}
@RequestMapping("/postData")
public @ResponseBody postData(Model model,
@RequestBody Comment comment,
HttpServletRequest request) throws Throwable{
String sendURL= request.getParameter("sendURL");
System.out.println(this.getClass() + " : comment : " + comment.toString());
/* Process data and formulate a response.... */
AjaxResponse ajaxResponse = new AjaxResponse(processedResponseText, processedResponseCode);
return ajaxResponse;
}
class CommentResponse extends Comment {
private long commentId;
private Timestamp entryDateTime;
// etc
}
class AjaxResponse{
private CommentResponse commentResponse;
private String responseCode;
//other stuff
}
|
Ajax Post to PHP and Get Return Data
Date : March 29 2020, 07:55 AM
To fix the issue you can do You seem to try to access "slideStatus" but you are posting "sliderStatus".
|
why ajax post method return empty alert box when i tried to return data from controller file in codeigniter
Date : March 29 2020, 07:55 AM
With these it helps you cant alert $("#myform").serialize(); because this is an object instead of alert it try console.log($("#myform").serialize();) then open browser console and see what is printed $.ajax({
type: "POST",
url: "<?php echo base_url(); ?>index.php/customer/viewCustomerData",
data: dataString,
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data);
}
});
|
Ajax POST data not appearing in modal
Date : March 29 2020, 07:55 AM
it fixes the issue Update: I did some tinkering and got the table data to show. In the stored procedure (GetDocUnderReviewDetails.cs) I commented out the line where DocBudgetId = @DocBudgetId and added where DocNumber = @DocNumber. I noticed that if I just commented out where DBI = then it showed all of the comments made for every modal, which was an issue I was dealing with earlier. namespace BB360
{
public partial class Sql
{
public const string GetDocUnderReviewDetails = @"
select
DocBudgetId,
DocNumber,
ReviewBy,
convert(varchar, ReviewOn, 101) as ReviewOn,
isnull(ReviewComment, '') as ReviewComment
from DocBudgetReview
--where DocBudgetId = @DocBudgetId
where DocNumber = @DocNumber
order by ReviewOn desc
";
}
}
public ActionResult GetDocUnderReviewDetails(Guid docBudgetId, string docNumber)
{
var data = Common.GetBBDbExecutor().Query<BudgetReportingViewModel.ReviewRow>(Sql.GetDocUnderReviewDetails, new {
docBudgetId,
docNumber
}).ToList();
return Json(data);
}
|
How to return data from an AJAX post from PHP?
Date : March 29 2020, 07:55 AM
|