cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Ajax post data into ContentLoaderFunctions.PostJSON ?

mak1
1-Visitor

Ajax post data into ContentLoaderFunctions.PostJSON ?

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?

5 REPLIES 5
philippk
10-Marble
(To:mak1)

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.

ptc-6292103
12-Amethyst
(To:mak1)

"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.

rhawa
12-Amethyst
(To:mak1)

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.

rhawa
12-Amethyst
(To:rhawa)

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)

rhawa
12-Amethyst
(To:mak1)

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.

Announcements
Top Tags