I am using echo to send a string back to a javascript function, however the length of the string sent is 1 less than its length as read by the js function. When I look at the first character in firebug console the value is “”.
echo "Success";
I perform a comparison on the data returned by the ajax call which fails:
if (data == "Success")
If I apply the jQuery $.trim function it succeeds.
I think that
echo()isn’t printing the null character, it is printing a return. Useprint()instead.To avoid this whole issue I would either use more RESTful HTTP status codes to represent success and failure (in which case jQuery even has specific onFailure and onSuccess callbacks) or use JSON to encode the values you are returning. You could have PHP do
echo json_encode(array('status' => 'success'));and then in jQuery doif(data['status'] == 'success'). This will make your code more future-proof if you decide to return actual data later. If you do the later, make sure to either have your content type set correctly or tell jQuery to expect JSON.