javascript - Best Way to Make AJAX Call from WebForms Page - Stack Overflow

admin2025-04-19  0

Is there a remended way to make an AJAX call within a WebForms application?

  1. I know there is the built-in ASP.NET AJAX ponents, but they seem a bit heavy. I'm used to doing this in MVC and it seems very clean.

  2. I can use Page Methods, but they require that my method is static, which makes it more difficult to access my database, etc.

  3. I assume I can also just use jQuery to make the call, although attempts at this have failed in the past, usually due to problems with the way data was returned (JSON, etc.).

My goal is get pass a string fragment and get back a list of items that match that fragment. Speed would be nice. Can I get some remendations?

Is there a remended way to make an AJAX call within a WebForms application?

  1. I know there is the built-in ASP.NET AJAX ponents, but they seem a bit heavy. I'm used to doing this in MVC and it seems very clean.

  2. I can use Page Methods, but they require that my method is static, which makes it more difficult to access my database, etc.

  3. I assume I can also just use jQuery to make the call, although attempts at this have failed in the past, usually due to problems with the way data was returned (JSON, etc.).

My goal is get pass a string fragment and get back a list of items that match that fragment. Speed would be nice. Can I get some remendations?

Share asked Oct 2, 2013 at 15:59 Jonathan WoodJonathan Wood 67.5k82 gold badges304 silver badges532 bronze badges 1
  • I prefer jQuery's AJAX, although ASP.NET typically has something to say about how I format my data. Post what you've tried with this that failed in the past, I'm sure we can figure it out! – tymeJV Commented Oct 2, 2013 at 16:02
Add a ment  | 

3 Answers 3

Reset to default 4

Use an HTTP handler (.ashx), this will give the page instance and flexibility of it being script callable via jQuery .ajax() method, like this:

$.ajax({
    url: "Handler/MyHandler.ashx",
    contentType: "application/json; charset=utf-8",
    data: { 'Id': '10000', 'Type': 'Employee' },
    success: OnSuccess,
    error: OnFail
});

function OnSuccess() {
    // Do whatever needs to happen on success here
}

function OnFail() {
    // Do whatever needs to happen on failure here
}

I always use JQuery AJAX calls:

$.ajax({
  url: 'your url',
  headers: { headertitle: headerdata},
  cache: false,
  success: function(data) {
    //success function. Returned data is stored in the data variable.
  }
});

Let me know if you have further questions.

I'll throw my vote in with the jQuery AJAX calls.

But in my years doing WebForms, calling code-behind file methods wasn't the preferred way. I'd expose the functionality as a Web Service. You can do it as WCF or WebAPI (ing from MVC, WebAPI may be a better fit for your expertise).

If it's a one-off function call to do the AutoComplete functionality (I'm assuming based on the problem you describe), you can probably get by with calls to code-behind methods, and you could use the AjaxControlToolkit's AutoCompleter control. But if you notice you're doing more and more calls via AJAX, you need to consider putting your service calls into a true Web Service.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745058079a282525.html

最新回复(0)