Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X
The following ajax code is used to find closest stations.
jQuery.ajax(
{ type: 'POST',
url: 'http://api/server/search.php',
data: { 'apikey': ApiKey, 'apiversion': '3', 'action': "search", 'type':'near', 'lat':'59.91673', 'long': '10.74782', 'distance': '2000', 'limit': '10' },
success: printJsonResponse,
dataType: 'json' }
);
-----------------------------------------------------------------------------
var apiKey = "123";
var params = {
url: "http://api/server/search.php" /* STRING */,
content: {'apikey':apiKey, 'apiversion':'3', 'action': 'search', 'type':'near', 'lat':'59.916', 'long':'10.75', 'distance':'2000', 'limit': '10'} /* JSON */,
timeout: 60 /* NUMBER */
};
var result = Resources["ContentLoaderFunctions"].PostJSON(params);
But I get the answer "The API is not valid". Could anyone help me how to convert the above JQuery.ajax code into correct PostJSON params?
Have you tried to use double quotes ("teststring") in the JSON content ?
To my knowledge JSON standard specifies double quotes for strings.
Not sure how tolerant the Thingworx ContentLoaderFunction for PostJSON is.
"The API is not valid" looks like an error from the remote server, so the request is being sent successfully by Thingworx. Maybe double-check your api key and api version.
It looks like the APIkey you provided here does not work.
I've requested an APIkey from there and will continue testing once I got it.
BTW: I tried it like Philipp suggested (with " instead of ') and I am getting at least a response from the remote server (APIkey not valid)
Now that I got an API Key I was able to test it.
I've tried it both with "Advanced REST Client" as well as directly with ThingWorx, in both cases I was not able to post the content as JSON to the request (as payload).
The only way that I got response was to compile the request completely into the URL:
..../api/server/search.php/?apikey=123&apiversion=3&action=search&type=near&lat=59.916&long=10.75&distance=2000&limit=10
Then I was able to get the response as JSON. That means for the Request Method it does not care if you are using GetJSON or PostJSON in ThingWorx (you are just using the URL parameter).
That means for a generic service you have to compile the URL string with appropriate input parameters.
Depending on the further usecase you may want to convert the JSON to an infotable afterwards.