Jquerry Ajax post Error State 0 (HTTP IN)

hello friends,
i have an imp receiving data from an HTTP IN node. in order to push data into the node i first used an HTML form.


Value: `

i stopped using this method because when i submited a string, the page was reloaded, but i got an “OK” so it was working.
in order to avoid this i tried to setup an jQuery ajax post so that i could handle the response and disable the page reload when the button was clicked. this is my code:
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script> $(function(){ $("#contact_form").submit(function(event){ event.preventDefault(); var command = $("input#command").val(); var send = 'value=' + command; $.ajax({ type: 'POST', url: 'https://api.electricimp.com/v1/_hidden_', data: send, success: function(response,status,object) { alert("sucess"); }, error: function(jqXHR, exception) { //alert(jqXHR.responseText); if (jqXHR.status === 0) { alert('Not connect.\ Verify Network.'); } else if (jqXHR.status == 404) { alert('Requested page not found. [404]'); } else if (jqXHR.status == 500) { alert('Internal Server Error [500].'); } else if (exception === 'parsererror') { alert('Requested JSON parse failed.'); } else if (exception === 'timeout') { alert('Time out error.'); } else if (exception === 'abort') { alert('Ajax request aborted.'); } else { alert('Uncaught Error.\ ' + jqXHR.responseText); } }, }); }); }); </script>

and in the body of my page i have the simple text input:




with this setup i CAN send the string to the imp, i know this i’ve tested it and the http in node receives the string. HOWEVER i’m not getting a sucess on the ajax post, instead, i’m getting inside the “error” function. in this function with my “if else” i found that i am always getting the STATUS = 0.
i’ve tryed searching for an explanation and this can occur if you send an ajax call and a refresh of the browser was trigger before getting the ajax response, the ajax would be cancelled and i would get this status. BUT in the beggining of my function with is handling the submit button i state: event.preventDefault();
i hope this is something simple that i am missing. and sorry to bring this up to you guys, but i’m not familiar with the language and i really can’t see what’s going on.
thanks in advance

If your using chrome go to tools/javascript console I wonder if your seing a cross origin request failure in red text. That might be causing the status. You can request to go on to Agents for handling HTTP coms beta by PM Matt the bearded inventor on forum. I

thank you for your suggestion, you were in fact correct, i have the following message in the javascript console:
XMLHttpRequest cannot load https://api.electricimp.com/v1/hidden
Origin null is not allowed by Access-Control-Allow-Origin.

does this mean that i cannot receive the response from the imp cloud? why does it work with the HTML form?

and regarding you sugestion on agents, how can they be of assist in order to solve this communication failure?

thank you


i’ve been searching for error reported by the javascript console, and it’s associated with the cross-domain communication, wich for what i have seen it’s not supported for security reasons (don’t know if this is correct). the solution presented was to use JSONP so that the response was encapsulated and could reach the origin of the request. is this why you suggested me to use the Agents? can i respond with JSONP if i use agents?

thank you

Agents replace HTTPin and you can write your own HTTP handlers. So you can use Jsonp but better still handle CORS requests. I’ve written a number of JQM based tutorials that works with Agents you’re free to re-use any code.

thank you for your help
just sent a PM to Matt to get acess to the agents.
will look into it.