How to Pass Ajax values to py?

How To Pass the JavaScript values to the python class using AJAX?.Please Anyone Help me.

You can append values to the query string, and reason them from the python side :+1:

Sir Can you Explain well Please.i'm a beginner

Yes, I can try, but please, remember that being a beginner means reading and doing lots of research on your own as well :wink:

If you want to pass a value from some JavaScript to your Python backend, you can do it with the query string, i.e. /path-to-my-python-script?custom_code=123&another_value=that-is-a-string

If you have some python at /path-to-my-python-script you can inspect the request, and there you will find the custom_code and another_value keys and their values.

2 Likes

Lets say you have a template that is supposed to show 'something'.

Typically, this can be the template (view) for a custom content type.
If you make this view with 'plonecli', you get both a 'template.py' and a 'template.pt'.
Probably you should use plonecli for what you are trying to do

It will also make some code to 'use some variable'.

If you edit the 'templatename.py', you should be able to read parameters passed to it like this:

 custom_code =  self.request.get('custom_code')  # will be 123 in example above

Usually, you write a browser for retrieving parameters from the query string like

from Products.Five.browser import BrowserView

class MyView(BrowserView):
    """
    Example browser view to retrieve 'a' and 'b' from the request.
    """

    def get_values(self):
        """
        Retrieve 'a' and 'b' from the request and return them.
        """
        a = self.request.get('a', None)
        b = self.request.get('b', None)
        return a, b

Please refer to the Plone training and don't try to tinker around without having understood the basis of browser views in Plone

$.ajax({
url: " ""what comes here""" ",
method: "POST",
data: {
data: data,
},
success: function(response) {
console.log("Success");
console.log(response);
},
error: function(xhr, status, error) {
console.error("Some Errors");
console.log(xhr.responseText);
}
});
}

Is this Correct?if not please correct

Someone that know this better / can explain better should probably answer (but since nobody has yet):

  • In plone there are many 'views'.
  • A view can return different information / template, even on the same content.
  • So, you have have several views for http://mysite/mycontent
  • Some of these view you can see in the 'display' menu in Plone
  • You can also call the view by url, for example http://mysite/mycontent/myview
  • The easiest way to make a new view is to use plonecli, and add the add-on it creates to your site.
  • You can make a view 'only with python' and let it return whatever you want.
  • It is also possible to use existing views
  • it is also possible to use plone's restapi.

The restApi can return 'a lot of things', maybe that is a good starting point?