views:

24

answers:

1

I am working on a project where I have to send an ajax request to some java based middleware that someone else is in charge of. 4 things can happen in response to the data I POST back...

  1. the userid is correct so the response I receive includes:

    XMLHttpRequest.responseText = "someurl.html"
    XMLHttpRequest.status = 200

  2. the userid is correct but the person does not match a certain status:

    XMLHttpRequest.responseText = "wrongstatusurl.html"
    XMLHttpRequest.status = 200

  3. the userid is correct but the captcha is wrong so the response I receive includes:

    XMLHttpRequest.responseText = "Error. Captcha wrong."
    XMLHttpRequest.status = 200

  4. the userid is not correct:

    XMLHttpRequest.responseText = "Error. Not a user."
    XMLHttpRequest.status = 200

So as you can see each response is 200 OK. This means every ajax request hits my "success" callback (I'm using jQuery for this bit and it has a built in success callback as part of its ajax method) so I need to base my callback handling on the actual text I receive back. Correct?

Either that or I need to get the middleware guy to send back status codes that better match the actual status of the request... does that sound right?


My Question:

What is the best way to proceed here? Just handle the plain text that is returned (suggestions on how to best do that would be most appreciated... just parse the text? Seems kind of inflexible... what if the text changes some day?) or get some other sort of response back (suggestions on what to tell him I need most appreciated... just say, I need some kind of header that specifies the status code... maybe?). How would you handle this?

+1  A: 

I think the best way would be to parse the responseText and hope that they never get changed.

If you were in charge of the actual pages/data being returned you'd have more flexibility. The way I would typically have written that end (the end you have no control over) is to return some sort of XML/JSON always. This way if the status was 200, you'd get some data (regardless of an error in the processing, vs page not found or something). Within that data I typically throw a tag (true or false) and an tag with the data for any error message. If there were no error I'd return the rest of the data afterwards.

I would then use the responseXML and iterate over the nodes, check out my status/error nodes and alert the user to the error (simply by displaying the error node text which would be the message). If there were a success, I would continue processing the remaining nodes depending on how I needed them. It would be a little easier now with JSON and jQuery's support for it within the ajax methods.

pinkfloydx33
Thanks for the response. As it stands I am requesting that they change the response to at least just return a number: 1,2,3,4 so I can branch based on that. I think it would be more future proof than trying to parse the text. Not sure if they will grant my request but it seems easier to do it that way to me.
rg88
Oh, I think parsing the text is absolutely HORRIBLE. But given what you've got it looks like it is your only choice. That is unless they hope agree to make some changes for you. The problem with returning numbers is that it is still up to you to know what these numbers mean, and if they change meaning and you aren't informed its just as bad as the original responseText changing.
pinkfloydx33