How to call CURL command line in Jqeury?

Hi all,

I am trying to call the curl comand in Jquery on front end. similar like

# Using curl in your terminal
curl https://api.spark.io/oauth/token -u spark:spark \
     -d grant_type=password -d username=joe@example.com -d password=SuperSecret

But , i am not exactlly known how to make the code , below is what my tried:

$.ajax({
		type:	"POST",
		url:	"https://192.168.1.100:5683/oauth/token",
		beforeSend: function(xhr) { 
	    	xhr.setRequestHeader("Authorization", "Basic " + btoa("spark:spark")); 
	    },
		data:	"grant_type=password&username=AAA@gmail.com&password=abc",
		dataType:	'json',
		success: function (data) {
	      alert(JSON.stringify(data));
	    },
	    error: function(){
	      alert("Cannot get data");
	    }
});

Is there anybody faced to this problem , or give me some sugguestion ?

The error message will be

Failed to load resource: net::ERR_SSL_PROTOCOL_ERROR

Where are you sending the POST to? Are you still connected to the Spark Cloud, or are you hosting your own local cloud?

I’m using the following code: Perhaps you can use some of it.

var deviceID    = "<<insert device ID>>";	
var accessToken = "<<insert accesstoken>>";	
var baseURL = "https://api.spark.io/v1/devices/";
var funcKey = "<<insert function name which you'd like to call>>"
var url = baseURL + deviceID + "/" + funcKey;

$.ajax({
type: "POST",
url: url,
data: {
    access_token: accessToken,
	args: <<insert your data which you like to send>>
	},
dataType: "json"
})
1 Like

Hi @Moors7 , thanks your reply,

what i used cloud url api will be successful access, the problem only happen on locally cloud call.

@kennethlimcp, what is my wrong for it , i guess, the locally cloud does not support spark cloud api ? if so , could give me some instructions for exactlly differences between locally and remote cloud ? I could nt find this in website.

@yuanetking,

You can see what’s available in the spark-server here: