Response from external url failed

I want to get the response from an external url , which is working if I load the url in web browser and postman as well. But it is not working in erpnext. I have created a custom script to pull the response from the url as given below :

frappe.ui.form.on('Doctype', {
	 refresh: function(frm) {
	 	frm.add_custom_button(__("Fetch"), function() {
        	 $.ajax({ 
                type : "GET", 
                url : "http://localhost:5000",
                dataType: 'jsonp',
                beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'token [api key]:[api secret]')},
                success : function(result) { 
                    console.log('Success:',result);
                }, 
                error : function(result) { 
                    console.log('Fail:',result);
                } 
            }); 
             })
	}
});

It is throwing error response, where is the problem?

@Ryan1 where is erpnext installed ?

On the same system @bahaou

@Ryan1 share the error messages. and why don’t you use python to get api responses ?

Screenshot from 2023-09-01 14-17-46

Need at client’s end so not called the response using python. @bahaou

@Ryan1
image
I just tried your code and it works . so I am guessing the problem is the api authentication as your readystate indicates 4. are you sure about the url and the authentication in the header ? have you tried postman to test it ? try to remove the datatype . it was giving errors in my case .

Can you get a response from any random site other than erpnext? @bahaou

@Ryan1 yes, but test it using postman first.

It works in postman but not in erpnext custom script. @bahaou

@Ryan1 show the headers section


@bahaou

@Ryan1 what is the authorization field you wrote in the header of the call ? because I can’t see it in postman .

Authorization is not needed actually, the site ‘http://localhost:5000’ (running in flask, not a part of erpnext) simply throws value 220 which I need to fetch in erpnext doc. @bahaou

then remove it , and remove the datatype , it’s not json in your case

Screenshot from 2023-09-01 16-15-12

Then it throws this error CORS with fail response. @bahaou

@Ryan1 try datatype json . without header

Screenshot from 2023-09-01 16-21-07

Tried …same problem @bahaou

@Ryan1 try to add this header

 headers: {  'Access-Control-Allow-Origin': 'http://The web site allowed to access' },

same error @bahaou

@Ryan1 I really can’t do much more about this . just write python code and use js to call that python code. that would work better anyways .