Reputation: 19903
After a post with "$.ajax()
", I sometimes receive an exception from the server. You can see a part of the returned HTML below:
<html>
<head>
<title>MyMessage</title>
<style>
/* ... */
</style>
</head>
<body bgcolor="white">
<span>
<H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>
<h2> <i>MyMessage</i> </h2>
</span>
<b> Exception Details: </b>System.Exception: MyMessage<br><br>
<!-- ... -->
[Exception: MyMessage]
lambda_method(ExecutionScope , ControllerBase , Object[] ) +159
...
</body>
</html>
I'd like to get the value of the title tag ("MyMessage") How can I do this with jQuery?
Upvotes: 2
Views: 1129
Reputation: 268364
In short, I don't think you can. jQuery only allows you to select tags that can be placed within a div tag. Your title tag doesn't qualify.
See this answer for more information.
Partial HTML:
There are other options. You could return partial HTML whenever you experience an error. Instead of <html><body><p>Error</p></body></html>
you return <p>Error</p>
.
JSON:
You could also go with something a bit cleaner, like JSON. Since you're working with ajax anything, you could send back the following string:
{'status':'failed','message':'Something bad happened'}
And then access the 'message' value within your client-side script.
Proxy Script:
By using a proxy script written in either C# or PHP you can get access to the value of a title field, and return that to your jQuery ajax process.
IFrame Method:
Load the results up in an iframe, and then get the value through iframe.document.title
. You can make your iframe 1x1, so it's not visible.
Upvotes: 4