0

I'm making an android app using apache cordova and I would to know, if it's possible, how to run an url using javascript. I want to update a data using an url like this : http://login:password@192.168.*.*:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=22

When I execute this URL in the navigator, the data is updated, now i would like to know if I can do that using javascript/jQuery. I use $.getJSON on the same fil and everything works.

I've already tried

$.get("http://login:password@192.168.*.*:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=22")

and

$.ajax({"http://login:password@192.168.*.*:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=22"};)

Cœur
  • 37,241
  • 25
  • 195
  • 267
saroten
  • 95
  • 1
  • 2
  • 10
  • No, i don't need and i don't know what's json.htm. I just use the API, i didn't crete it. – saroten Jan 13 '16 at 14:19
  • Possible duplicate of [HTTP GET request in JavaScript?](http://stackoverflow.com/questions/247483/http-get-request-in-javascript) – Miki Jan 13 '16 at 14:28
  • Your question is unclear. You might want jQuery's `$.ajax` `$.get` `$.load` `$.post` ...etc... Look through the jQuery docs online and decide which of those best meets your needs. – John Hascall Jan 13 '16 at 14:37
  • the problem is that none of them work – saroten Jan 13 '16 at 14:38
  • If you're having cross domain issues then you'll need to refer back to the API provider and check whether or not JSONP is available. Can you tell us what the API is, as obviously 192.168.*.* doesn't give us any info – Reinstate Monica Cellio Jan 13 '16 at 14:56
  • The API creator is Domoticz, i don't think it helps you and i can't give the complete URL cause login and password are needed. – saroten Jan 13 '16 at 15:10

3 Answers3

0

If json.html is an serverside-script you can use

$.get("yourPath/json.htm?key=value");

or

$.get( "test.php", { key1: "Value1", key2: "Value2" } );

but when it is an js-script in a html-file, you have to create an iframe or a new window.

leguano
  • 171
  • 1
  • 6
0

in your php:

// process data
// $_GET['type'], $_GET['param'] ...
$result = json_encode(array("key"=>"value"));
$callback = $_GET['callback'];
header('Content-type: application/javascript');
echo "$callback($result)";

in your js:

$.getJson('yourpath.json?callback=?&type=command&param=udevice&idx=4&nvalue=0&svalue=22',function(data){
    console.log(data);
});

notice the callback=? in the url to make jQuery treat it as a JSONP request. here is the documentation: http://api.jquery.com/jquery.getjson/

rabbit.aaron
  • 2,369
  • 16
  • 25
0

You can try:

$(document).ready(function() {
  var grid_refresh = 'https://login:password@192.168.*.*:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=22';
  var data = $('body').load(grid_refresh);
  console.log(data);
});
Yaseen Ahmad
  • 1,807
  • 5
  • 25
  • 43
Vishal Kumar
  • 1,290
  • 14
  • 15
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Rosário Pereira Fernandes Feb 17 '18 at 23:40