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

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Unable to get proper response in ajax call, returning HTML response instead json.

Janakiraman-P
14-Alexandrite

Unable to get proper response in ajax call, returning HTML response instead json.

Hello,

 

I am trying to bring a custom chart  in thingworx, It is a out of the scope of thingworx widgets so am developing it  using HTML,JS and viewing through webframe widget. Whenever I call thingworx services it is returning HTML response instead json. Please find the sample code below.

 

 

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script>
</head>
<body>
<button onclick="callme()">click</button>
<p id="results">check here</p>
<script>
function callme(){
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
accept: "application/json;charset=utf-8",
url : 'https://localhost:8443/Thingworx/Things/test/Services/parseData',
success: function(response){
console.log(response);
$("#results").append(response.result);
}
});
}
</script>
</body>
</html>

 

Here, The service parseData() is returning HTML response though the accept type json. I could able to get json response in postman.

 

Please let me know what went wrong.

 

Regards,

Janakiraman P

 

ACCEPTED SOLUTION

Accepted Solutions
Janakiraman-P
14-Alexandrite
(To:tmisner)

Hello ,

 

Thanks for your response. I gone thru some workaround and got relevant solution.

 

 var chartData;

$.ajax({
type: 'POST',
url : 'https://<domain>:8443/Thingworx/Things/test/Services/getDataForChart',
dataType:"json",
crossDomain:true,
contentType:"application/json;charset=utf-8",
async:false,
success: function(data, textStatus, jqXHR)
{
chartData=data;
console.log(chartData.rows);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert("error");
}
});.

 

 

The above code works for me.

 

 

But I have another question here, how to call dynamic services like "setProperties" using the same procedure?

 

 

 

Regards,

Janakiraman P

 

 

View solution in original post

4 REPLIES 4

Hello Janakiraman,

 

Thank you for reaching out on the ThingWorx Developer Community.

 

Firstly, I do apologize for the delay in reply to your post. Were you able to workaround this as of yet?

 

If not, my initial recommendation is to simply try excluding the charset=utf-8 from both contentType and Accept.

 

Should this not work for you please provide the contents of the response you are getting. Is it the expected response just in HTML form or is there perhaps an error being returned?

 

Regards,

Tyler Misner

ThingWorx Technical Support Engineer

Janakiraman-P
14-Alexandrite
(To:tmisner)

Hello ,

 

Thanks for your response. I gone thru some workaround and got relevant solution.

 

 var chartData;

$.ajax({
type: 'POST',
url : 'https://<domain>:8443/Thingworx/Things/test/Services/getDataForChart',
dataType:"json",
crossDomain:true,
contentType:"application/json;charset=utf-8",
async:false,
success: function(data, textStatus, jqXHR)
{
chartData=data;
console.log(chartData.rows);
},
error: function (jqXHR, textStatus, errorThrown)
{
alert("error");
}
});.

 

 

The above code works for me.

 

 

But I have another question here, how to call dynamic services like "setProperties" using the same procedure?

 

 

 

Regards,

Janakiraman P

 

 

Hello Janakiraman,

 

Glad to hear you were able to resolve this.

 

With respect to your other query regarding a dynamic service like setProperties can you give me an example of where this service would appear?

 

I was able find some good examples using REST with SetPropertyValues if this is similar to  what you are looking for:

Example 1

Example 2

 

Regards,

Tyler Misner

ThingWorx Technical Support Engineer

Hi @Janakiraman-P.

 

If one of the previous responses allowed you to find a solution to your problem, please mark the appropriate one as the Accepted Solution for the benefit of others on the community.

 

Regards.

 

--Sharon

Announcements

Top Tags