I’m a newbie in javascript, and I don’t really understand the error I’m having.
I’m working on a MVC3 website, which has to monitor an embedded system.
Here is the javascript code that is running :
function GetTemp() {
var test = "gTe";
$.ajax({
url: '@Url.Action("../Carte/Get")',
type: 'GET',
data: {test: test},
success: function (result) {
if (result.charAt(4) == 'a') {
$("#LumAct").text(result.substr(0, 4) + " %");
alert('a');
}
...
And here is the c# action that returns a string
public String Get(String test)
{
flag = TCPClient.SendData(test);
if (flag == "1")
{
try
{
value = TCPClient.ReceiveData();
}
catch
{
value = "Erreur";
}
}
else value = "Erreur";
return value;
}
The error I have is in firebug, which tells me :
TypeError: result.charAt is not a function
[Stopper sur une erreur]
if (result.charAt(4) == 'a') {
So, what haven’t I understood? According to me, I’m using an ajx function that sends a httpGet to the controller, which responds with a string. In javascript, I can work on a string like I did.
To use the string object, I haven’t added any library. Should I have done that? I haven’t found any information telling that.
Can you try this:
Also, as mentioned by @Musa, you should add a
dataTypeattribute to the AJAX call: