Skip to main content
1-Visitor
October 3, 2019
Solved

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

  • October 3, 2019
  • 2 replies
  • 8180 views

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

 

Best answer by Janakiraman-P

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

 

 

2 replies

16-Pearl
October 16, 2019

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-P1-VisitorAuthorAnswer
1-Visitor
October 17, 2019

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

 

 

16-Pearl
October 17, 2019

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

Support
October 28, 2019

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