POST Django form with AJAX and JavaScript - ajax

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){
...

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 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.

Return value in Jquery 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.

play framwork JsRoutes ajax form submit

im trying to submit a form via JsRoutes and ajax and i dont think that im getting to the controller ....
this is my routes
POST /submit controllers.Application.submit()
and this is my jsrouts function:
$('#submit').click(function() {
JsRoutes.controllers.Application.submit().ajax({
data : $("#contact-form").serialize(),
url: '#{Application.submit()}',
success : function(data) {
alert("Succsses");
},
error : function(err) {
alert("error");
}
});
});
this is what im sendig to the server
http://localhost:9000/?name=ajax&lastName=aaa&email=miko5054%40hotmail.com&phone=453534
what im doing wrong here ???
The #{Application.submit()} code is incorrect. It should be #{routes.Application.submit()}.
But if you use the javascript router JsRoutes.controllers.Application.submit(), you don't need to specify the url inside the ajax() call. You can just remove the url:... line and everything should work fine.

Why is this jQuery Ajax request failing?

The FCC recently made available a small set of API calls to access FCC data. In particular, I'm interested in the Consumer Broadband Test API. I'm trying to access this API through jQuery but am failing. Please let me know if I'm doing something wrong with my code or if this seems to be a problem with FCC's API.
If you visit this API request in a browser, it returns a XML response just fine: http://data.fcc.gov/api/speedtest/find?latitude=30.240236062827297&longitude=-97.64787337499999
So I've tried to load this data in jQuery using various methods:
var url = "http://data.fcc.gov/api/speedtest/find?latitude=30.240236062827297&longitude=-97.64787337499999";
$.ajax({
type: "GET",
url: url,
success: function(data) {
console.log("ajax: " + data);
}
});
$.getJSON(url, function(data) {
console.log("getJSON: " + data);
});
$.get(url, function(data) {
console.log("get: " + data);
});
In the Firebug console, all three requests show a 200 (OK) status, but the response body is empty. Also, the resulting console.log messages are:
ajax:
getJSON: null
get:
Am I doing something wrong here?
To work around the Same Origin Policy, you'll need to use JSONP. It is supported by the API. Add callback=? to the URL string in your .getJSON() call:
If the URL includes the string
"callback=?" in the URL, the request
is treated as JSONP instead. See the
discussion of the jsonp data type in
$.ajax() for more details.
So, something like this:
var url = "http://data.fcc.gov/api/speedtest/find?...&callback=?";
$.getJSON(url, function(data) {
// do stuff
});
References: http://api.jquery.com/jQuery.getJSON/
You can't make cross-domain calls using AJAX. It doesn't work like that.
What you probably want to do is to have your AJAX query URL be a local script on your own server, then have that script run a request for the API url (using cURL or something).

Resources