Return value in Jquery Ajax - ajax

I am working on jquery ajax. I am using the code as follow :
var url = baseurl +"/index.php/users/ajaxwixusers";
var dataStringfirst = 'wixuserid='+ instanceId;
$.ajax({
//dataType : 'html',
type: 'GET',
url : url,
data : dataStringfirst,
complete : function() { },
success: function(data)
{
alert(data);
window.location.href="http://localhost:81/customers/index.php/acappointments/appointuseroverlay/user_id/203";
}
});
In PHP file I am getting the value as :
echo $user_id = $wixuser->id;
I want to return ths value on success. When I am trying to alert the output of ajax.
It is returning the complete HTML of the file. How can I return only value instead of complete HTML?

It returned complete HTML because your app kept running and rendering the rest of the request after your echo command. You should add one line exit instead.
exit();

Ajax response always gives complete data as output from requested page and If you don't want to use HTML then why you are placing it there? If you want to get any specific value then Print that value only don't use HTML on that page.

Related

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"];

How to save URL entered in textfield in jsp page, to send it to controller

I have a jsp page which has form(customerDetailForm) containing textfield to store URL, which through ajax call is going to controller and then to DB.
When I save some URL,I am getting javascript error "400 bad request" .
I read somewhere I need to encode url before sending it as url in parameter.
I have 2 question
1. how to encode URL
2. how and where to save encoded url in customerDetailForm
Since finally I am calling "customerDetailForm.serialize()" before sending to controller
In below code "fbUrl" is a URL with many special character.
CustomerDetailForm is having 2 fields, both are required to be send, "fbUrl" and "userName".
Code snippet
$.fn.editFbUrl = function (IdentificationNo, customerDetailForm, fbUrl) {
$.ajax({
url: "/useradmin/customers/edit/fbUrl/"+ IdentificationNo,
data: customerDetailForm.serialize(),
type: “POST”,
dataType: "json",
error: function() {
},
complete: function(data) {
handleAjaxResponse(data);
}
});
return false;
};
Use encodeURIComponent() function to encode the URL string.
This function encodes the following special characters: , / ? : # & = + $ #
var encUrl = encodeURIComponent(fbUrl);
url: "/useradmin/customers/edit/"+encUrl+"/" IdentificationNo,

POST Django form with AJAX and JavaScript

I would like to get some data within a Django form
In order to do that, I define a javascript which is:
$(document).ready( function() {
$('#newcase_form').on('change', function() {
pathology_type = ($('input[name="pathology_type"]:checked','#newcase_form').val());
console.log(pathology_type);
$.ajax({
url:"/pathology/",
type :'POST' ,
data : {'pathology_type' : pathology_type},
success : function(data){
console.log(date.resultat);
}
});
});
});
It works, I can retrieve the parameter inside the form
but I am unable to post it in the URL i always have an Error 500 and the paramater is not send to the URL .
here is my URL.py
url(r'^pathology/(?P<pathology_type>[A-Z]{1})/', 'myapp.views.pathology'),
Inside my form, I have a submit with another ajax, so I send an ajax request to another URL
Am I wrong, or is it possible to post to 2 different URL in the same form?
Thanks in advance for your help
I am not sure to understand your problem, but it think your javascript and url.py are inconsistent.
If you keep your javascript, the url should be:
url(r'^pathology/', 'myapp.views.pathology'),
and then get pathology_type from request.POST of your view
If you keep your url, javascript should be:
...
$.ajax({
url:"/pathology/" + pathology_type + "/",
success : function(data){
...

Using form data to dynamically construct url using ajax

I have the following ajax code that takes in 3 variables and passes them to a php file, and this is all fine and dandy. I want to do a redirect using the value stored in one of these variables. I figured I could just do window.location.href = within the success in the ajax call, but for some reason, it doesn't seem to respond to the click, and simply does nothing.
Here is the ajax function, hope y'all can help!
$("#findItem").click(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
$.ajax({
type: 'get',
url: 'http://plato.cs.virginia.edu/~aam3xd/cabbage/closestBuildingWeb.php',
data: {
foodItemId: $('#foodItem').val(),
sentLongitude: position.coords.longitude,
sentLatitude: position.coords.latitude
},
success: function(data){
//$('#answer').html(data);
//the following line doesn't seem to be executing....
window.location.href = foodItemId+"/"+sentLatitude+"/"+sentLongitude;
}
});
});
I think you should use your url into that window.location
window.location.href = "www.example.com/"+foodItemId+"/"+sentLatitude+"/"+sentLongitude;

How to perform ajax call to doaction in ofbiz

I would like to use Ajax to perform an action in OFBiz without the page refreshing. The task will be like filters. When I choose any Checkbox it should perform some action and display the result in the very same page.
What are all the steps i need to do?
I would appreciate some sample code for controller.xml,javascript calling Ajax ..etc
Thanks.
You can use form submission through ajax on an event in your ftl's. Here's a sample code for ajax call from say an ExampleCreateParty.ftl:
$.ajax({
url: '<#ofbizUrl>addSecurityPermissionToSecurityGroup</#ofbizUrl>',
type: 'POST',
accepts: 'xml',
data: $("form#Permissions").serialize(),
success: function(e) { console.log(e);
var xmlDoc;
try {
xmlDoc = $.parseXML(e);
var error = $(xmlDoc).find("Message").find("Error").find("ErrorCode").text();
var errorMsg = $(xmlDoc).find("Message").find("Error").find("ErrorMessage").text();
if (error=='0'){alert(errorMsg);}
console.log(xmlDoc);
} catch (err) {
alert('Saved Successfully!');
}
},
error: function(e) { console.log(e); }
})
Here in response to the service called i.e. addSecurityPermissionToSecurityGroup you can specify the response in the controller.xml which you can get within the success key in the ajax call itself.
To see a full end-to-end example have a look at this OFBiz page
As you can see whenever you change the product configuration, the product price is updated with Ajax call. Then you can find out for yourself the freemarker file containing javascript and the controller doing calculations and returning it back.

Resources