开发者

Dealing with Responses to Ajax Requests

开发者 https://www.devze.com 2023-01-23 06:04 出处:网络
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...

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 tex开发者_运维技巧t 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消