3

I have this code (InDesign CS6), and it's not working as expected. I'm using Mac OS and I need to make the code compatible with Windows and Mac.

Trying to get a text/JSON over my localhost, and the socket return an empty string:-

function getData(host, path) {
    var reply = '';
    var conn = new Socket;
    var request = "GET " + path + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "\n";

    if (conn.open (host)) {
        conn.write (request);
        reply = conn.read(999999);
        var close = conn.close();
    }
    return reply;
}

var host = 'localhost:80';
var path = '/test/test/json.php';
var test = getData(host, path);
alert(typeof(test) + ' Length:' + test.length);

Edit: Finally I manage to find out what causing the problem. I create a VMware, and try to run the script, and it's working. Not sure why it doesn't work on my machine. Download Wireshark, and saw InDesign send the request, but something blocks the request from accessing the server. I will update if I able to detect what causing the block.

Arepie
  • 31
  • 2

2 Answers2

1

When it comes to Socket, I guess the simpliest is to take advantage of that script written by Rorohiko: https://rorohiko.blogspot.fr/2013/01/geturlsjsx.html

Or have a try with IDExtenso library: https://github.com/indiscripts/IdExtenso

I find those convenient as they deal with the inner socket mechanisms for you.

Loic
  • 2,173
  • 10
  • 13
0

You do not need to use a socket just to get JSON from your server.

Instead refer to the XMLHttpRequest documentation or just a library such as jQuery which greatly simplifies making Ajax calls for JSON.

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • Alexander, that might be true if I can use jQuery or normal javascript. This is related to InDesign Scripting, in which limited function is available. Anyway, thanks for the feedback. – Arepie Jul 21 '17 at 03:34