2

I want to hit a controller action by jQuery ajax and didn't make that action intentionally to show the error.

" Server Error in '/' Application.
The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /SoftwareCompany/HamdunSoft "

As the above error is coming through the ajax error.responseText

error: function(error)
                {
                }

The full error.responseText is in the below code snippet.

"
<!DOCTYPE html>
<html>

<head>
  <title>The resource cannot be found.</title>
  <meta name="viewport" content="width=device-width" />
  <style>
    body {
      font-family: "Verdana";
      font-weight: normal;
      font-size: .7em;
      color: black;
    }
    
    p {
      font-family: "Verdana";
      font-weight: normal;
      color: black;
      margin-top: -5px
    }
    
    b {
      font-family: "Verdana";
      font-weight: bold;
      color: black;
      margin-top: -5px
    }
    
    H1 {
      font-family: "Verdana";
      font-weight: normal;
      font-size: 18pt;
      color: red
    }
    
    H2 {
      font-family: "Verdana";
      font-weight: normal;
      font-size: 14pt;
      color: maroon
    }
    
    pre {
      font-family: "Consolas", "Lucida Console", Monospace;
      font-size: 11pt;
      margin: 0;
      padding: 0.5em;
      line-height: 14pt
    }
    
    .marker {
      font-weight: bold;
      color: black;
      text-decoration: none;
    }
    
    .version {
      color: gray;
    }
    
    .error {
      margin-bottom: 10px;
    }
    
    .expandable {
      text-decoration: underline;
      font-weight: bold;
      color: navy;
      cursor: hand;
    }
    
    @media screen and (max-width: 639px) {
      pre {
        width: 440px;
        overflow: auto;
        white-space: pre-wrap;
        word-wrap: break-word;
      }
    }
    
    @media screen and (max-width: 479px) {
      pre {
        width: 280px;
      }
    }
  </style>
</head>

<body bgcolor="white">

  <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1>

            <h2> <i>The resource cannot be found.</i> </h2></span>

  <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">

    <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
    <br><br>

    <b> Requested URL: </b>/Chemical/DyeingPartList<br><br>

    <hr width=100% size=1 color=silver>

    <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1038.0

  </font>

</body>

</html>
<!-- 
[HttpException]: A public action method &#39;DyeingPartList&#39; was not found on controller &#39;Menu.Controllers.ChemicalStore.ChemicalController&#39;.
   at System.Web.Mvc.Controller.HandleUnknownAction(String actionName)
   at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
   at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
   at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
   at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->"

I have turned the string error.responseText into object element from this answer .

var element = $(error.responseText);

and the element is like this

enter image description here

As we can see there is an element named title in the object in red box at position 2 indexed 1. I can find it's(title tag) innerHtml by any one of the following

 elem.get(1).innerHTML
 elem.get(1).text
 element[1].innerHTML
 element[1].text

But I want to find this value by Jquery in the below way. Because in future for some cases I may required searching elements in object by property name rather than the index value. please help me if it is possible.

$("title", element).html()
$(element).find("title").html()
3
  • Can you post the value of e.responseText Commented Aug 29, 2018 at 7:38
  • @MilindAnantwar it is already. please click "Show code snippet" the underlined blue text Commented Aug 29, 2018 at 7:57
  • Got it. posted the answer for same. Commented Aug 29, 2018 at 8:13

2 Answers 2

3

You can use $.parseHTML to convert html string to array of DOM nodes. Which can be used along with jquery selectors and function:

 var dom_nodes = $($.parseHTML(e.responseText));
 alert( dom_nodes.filter('title').text());
Sign up to request clarification or add additional context in comments.

6 Comments

"find" is not working for me, How about filter instead? var doc = $.parseHTML(error.responseText); var titleNode = doc.filter(function (node) { return node.localName === "title"; }); console.log(titleNode[0].textContent);
you are mixing javascript here. And the reason its not working is because of text / in html string. Other than that, above solution will work for you . jsfiddle.net/8sy4g03f
You used find earlier in your solution, filter works though (javascript way or jQuery way). I believe find is not working because it searches through the DOM and here the elements in responseText, even after converting to jQuery objects, doesn't exist in the DOM. Please correct me if I am wrong.
The reason find is not working is parseHTML is returning nodes list as array. And title is one of the node. Find will work when the selector node is child element.
but if I want to show any other tags like body, h1, head then it is not working. What to do then?
|
1

First, create a document from the string.

How to create Document objects with JavaScript

var doc = (new DOMParser).parseFromString(error.responseText, "text/html");
// first argument: html to be converted to doc
// second argument: mime_type (text/html or text/xml, it depends)

Second, you know the drill.

doc.querySelector("title").textContent;
// $(doc).find("title").html();
// The resource cannot be found.

Alternatively, use $.parseHTML and filter nodes.

var doc = $.parseHTML(error.responseText);
var titleNode = doc.filter(function (node) {
    return node.localName === "title";
});
console.log(titleNode[0].textContent);

2 Comments

thanks. But i want it by jquery. I have already got the value by JavaScript or jQuery Get Function.
If you don't want to create document, use $.parseHTML and then filter title node.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.