Javascript can not find page in Aspx

Asked

Viewed 88 times

0

I have an application in ASPX and C#. Inside the application’s "Scripts" directory, I have a js with the following code:

function RecuperaDados(valor){
  loadXMLDoc("../Views/Home/Contact.aspx?ID="+valor);
}

However, it is returning error "404 -Not Found", only that this page "Contact" is inside the directory "Home" which is inside "`Views". What could I be doing wrong ?!

1 answer

1

You’re making a GET type HTTP request, which means specifying a file path is illegal and won’t work.

Therefore, its function is loadXMLDoc will not get another return until you specify an action.

How so?

If you don’t already have it, then create a method for Contact in your controller Home. Something like that:

public class HomeController : Controller
{
    [...]

    public ActionResult Contact(int id)
    {
        return View(id);
    }
}

Then, in your loadXMLDoc function, pass as parameter "/Home/Contact?id=" + valor, thus:

function RecuperaDados(valor){
  loadXMLDoc("/Home/Contact?id=" + valor);
}

What happened? What’s the difference?

GET requests made with AJAX request for an equivalent type return - HTTP, in this case -, and you were dealing with a file address, which, as already said, will not work. So we create an action on your controller Home, calling for Contact, to render your view Contact.aspx (you’ve seen Razor?) as a response to a GET request in the HTTP protocol.

In this way, we were able to solve your problem by meeting the specification of the AJAX request.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.