My question is related ajax request. I am new to ajax . So i want to know taht how to pass data in ajax dynamically? - ajax

Below code is of my ajax request where i pass return_id in data which is not dynamic. So how can i pass return id dynamically?
$ ->
$('#manualShipment').click ->
$.ajax
type: 'POST'
datatype: 'json'
data:
return_id: 274588
url: '/returns/create_shipment_manually'
success: (data) ->
$('#manualShipment').disabled = true;
$('.tracking_detail').show()
I tried to take id from view of rails but it won't work. So what would be correct way to pass data dynamically in ajax post request

Related

Flask Ajax POST data scope

I need to render a Flask template but the ajax data is only accessible within the POST if statement and does not show when I call a get direct after a posted the data.
I have a working ajax here
$.ajax({
type: 'post',
url: "/query",
dataType: 'text',
data: JSON.stringify({hostname:hostname, bf_id:computerID}),
contentType: 'application/json;charset=UTF-8',
success: function () {
window.location.href = "/query";
}
});
});
The data is successfully posted and the redirect is working. But when the redirect calls the function to render the template, the posted ajax cannot be retrieved.
#app.route('/query', methods=["GET", "POST"])
def query():
hostname=""
if request.method == "POST":
#these values only exist in if statement
hostname = request.json['hostname']
bf_id = request.json['bf_id']
return render_template('query.html', hostname=hostname)
Am I using an incorrect work flow?
Am I using an incorrect work flow?
Yes.
You make the POST request with the data.
You get a response which does things with that data
You then make a GET request without the data
You get a response which can't do things with the data that it doesn't have
The point of Ajax is to make an HTTP request without loading a whole new page.
If you want to load a whole new page, then use a regular form submission without involving JavaScript at all.

How to set s:param value in Ajax success body in Struts 2

How can I set s:param value in Ajax success body?
I load a data with Ajax call and I fetch it into datatable
but when I want to set s:param value I can not get its value, below is my code:
$.ajax({
url: "dataPp",
type: 'POST',
dataType : 'JSON',
success: function (res) {
table = $('table#dttable1').DataTable();
table.clear();
$.each(res.dokpp, function(i, item){
var json = res.dokpp[i];
table.row.add(
[json["kodeDok"],
json["fileNameUi"],
json["depPenerbit"],
json["createdDate"],
json["tglBerlaku"],
json["tglKadaluarsa"],
json["urutRev"],
'<s:url var="prev" namespace="/mr" action="prevDasboard">'+
'<s:param name="file">'+json["fileName"]+'</s:param>'+
'</s:url>'+
'preview'
]);
console.log(json["fileName"]);
});
table.draw();
}
});
I fixed this with this code :
'preview'
I use html tag to create a href.
When you use <s:param> tag the value is not yet available.
Struts tags are executed on server when JSP is rendered, but ajax is a javascript code which is executed on the client's browser after response is returned from the server. The server can't know what the value is substituted by the client.
You can render url without parameter and then add it dynamically.
var url = '<s:url var="prev" namespace="/mr" action="prevDasboard"/>?file='+json["fileName"];

Manually adding google analytic code to different urls through jQuery AJAX

I'm working on a progressive page loading script where new pages will load continuously when bottom of the page is reached. I'm using ajax to fetch the data dynamically through jQuery ajax from the server, the return data will return the url of the new post as well as its content.
My problem is what code should I use to submit my returned post url to analytic so that it registers as a valid page view?
$.ajax({
type: 'GET',
url: 'http://somedomain.com/fetch_pages.php',
data: { 'page': this.$loadMoreCounter , 'section': that.$section },
dataType: 'json',
success: function (data) {
//analytic code upon each successful callback.
});

Rails allow POST for Ajax on index action without breaking CREATE

I have a simple setup for a resource, currently in routes.rb I have:
resources :leave_requests
This works exactly as expected, on the index view I have a datatable setup using Ajax via a GET, this is also working but the URI is getting very large during the Ajax request. I would prefer this to be a POST action, for this to work I require a POST instead of a GET on the index action in my routes.
However, this will break the CREATE action i.e. will simply load the index page on submitting a new request. i.e. If I do this:
post '/leave_requests', to: 'leave_requests#index'
resources :leave_requests
How can I get these to co-exist happily?
Have you try to do something like in this Rails , Ajax , Post Function , Jquery the code looks something like
$('form').submit(function()
{
var myForm = $('form').serialize();
$.ajax
({
url:'/leave_request/create',
type:"POST",
dataType:'json',
data: myForm,
processData:false,
success: function (msg)
{
alert(msg);
},
error: function (xhr, status)
{
alert(xhr.error);
}
});
});
you can also take a look at jQuery post to Rails

post form with ajax and additional json data

I send a form via jquery ajax. I do not only want to send the values of the formcollection I also want to send another id which is hidden in the page.
How can I send the form data + this id? Maybe there is a way to create one json object and I put everything in it?
jQuery('#myForm').live('submit',function(event) {
$.ajax({
url: 'Url.Action("Create")',
type: 'POST',
data: $('#myForm').serialize(),
success: function( data ) {
}
});
return false;
});
If your hidden field is already in the form, It will be in your serialized data. You dont need to send it exclusively. If you still want to send another piece of data from outside of your form, you may send it as a querystring value and have your action method a parameter named itemId.(Assuming hdnID is the id of your hidden element)
$.ajax({
url: '#Url.Action("Create","Home")'+?itemId='+$("#hdnID").val(),
type: 'POST',
data: $('#myForm').serialize(),
success: function( data ) {
}
});
http://api.jquery.com/serialize/

Categories

Resources