Announcement

Collapse
No announcement yet.

Ajax request not working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Ajax request not working

    I have created a custom button in contact and trying to call ajax on click event but not getting any response.

    Espo.define('custom:views/contact/detail', 'views/detail', function(Dep){
    return Dep.extend({
    setup: function(){
    Dep.prototype.setup.call(this);

    this.addMenuItem('buttons', {
    name: 'DocStatus',
    label: 'Doc Status',
    style: 'primary',
    acl: 'edit',
    action: 'GetDocStatus'
    }, true);
    },

    actionGetDocStatus: function(){
    this.notify('Wait... fatching the document status.');

    var url = 'https://ipinfo.io/json';
    var data = '';
    var options = '';

    Espo.Ajax.getRequest(url)
    .then(response => {
    console.log(response);
    })
    .catch(xhr => {});

    }
    });
    })​

    Above link in working and giving output in postman and there in EspoCRM Error 404 is showing.
    Please help me on this one, Any help is appreciated, Thanks in Advance​
    Last edited by conceptmedical; 04-26-2024, 10:32 AM.

  • #2
    Hi,


    It's better if you develop a custom button for this purpose.


    what would you like to accomplish after making this call?

    Comment


    • #3
      Hi Kharg, thank you for your prompt response.
      With the reference link only I have created that but ajax is not showing the response.

      The button is showing perfectly but on click event it should give the output in console but its giving error.

      Click image for larger version

Name:	image.png
Views:	175
Size:	9.2 KB
ID:	105439

      And the ajax URL changed to my Espo CRM URL + Ajax URL

      Click image for larger version

Name:	image.png
Views:	145
Size:	6.2 KB
ID:	105440
      ​​

      Comment


      • #4
        This should be working:

        PHP Code:
        define('custom:views/contact/detail''views/detail', function(Dep) {
            return 
        Dep.extend({
                
        setup: function() {
                    
        Dep.prototype.setup.call(this);
                    
        this.addMenuItem('buttons', {
                        
        name'DocStatus',
                        
        label'Doc Status',
                        
        style'primary',
                        
        acl'edit',
                        
        action'GetDocStatus'
                    
        }, true);
                },
                
        actionGetDocStatus: function() {
                    
        this.notify('Wait... fetching the document status.');
                    var 
        url 'https://ipinfo.io/json';
                    var 
        model this.model;

                    
        fetch(url)
                        .
        then(response => {
                            if (
        response.ok) {
                                return 
        response.json();
                            } else {
                                throw new 
        Error('Error:'response.status);
                            }
                        })
                        .
        then(data => {
                            
        model.set('name'data.ip);
                            
        model.save();
                        })
                        .catch(
        error => {
                            
        console.error('Error:'error);
                        });
                }
            });
        });
        ​​ 

        be ware that the forum may append invisible characters.

        We can't use espo.ajax as it preappends an API base path api/v1 to each request URL.

        I do remember that there was a way to avoid it but I can't find it.



        P.S. Usually it's better to perform these actions using a php controller or an API action and then you can use Espo.ajax to call your endpoint.
        Last edited by Kharg; 04-26-2024, 11:53 AM.

        Comment


        • conceptmedical
          conceptmedical commented
          Editing a comment
          Kharg, can you tell me where I can apply the parameters like username and password in this case.

      • #5
        Originally posted by Kharg View Post
        This should be working:

        PHP Code:
        define('custom:views/contact/detail''views/detail', function(Dep) {
        return 
        Dep.extend({
        setup: function() {
        Dep.prototype.setup.call(this);
        this.addMenuItem('buttons', {
        name'DocStatus',
        label'Doc Status',
        style'primary',
        acl'edit',
        action'GetDocStatus'
        }, true);
        },
        actionGetDocStatus: function() {
        this.notify('Wait... fetching the document status.');
        var 
        url 'https://ipinfo.io/json';
        var 
        model this.model;

        fetch(url)
        .
        then(response => {
        if (
        response.ok) {
        return 
        response.json();
        } else {
        throw new 
        Error('Error:'response.status);
        }
        })
        .
        then(data => {
        model.set('name'data.ip);
        model.save();
        })
        .catch(
        error => {
        console.error('Error:'error);
        });
        }
        });
        });
        ​​ 

        be ware that the forum may append invisible characters.

        We can't use espo.ajax as it preappends an API base path api/v1 to each request URL.

        I do remember that there was a way to avoid it but I can't find it.



        P.S. Usually it's better to perform these actions using a php controller or an API action and then you can use Espo.ajax to call your endpoint.
        On above example perameter data is not passed, I have checked with shared ajax link but its giving 404 error. Kharg, can you help me to get the ajax response using perameter(data).

        Comment

        Working...
        X