In your action method, return Json(object) to return JSON to your page.
public ActionResult SomeActionMethod() { return Json(new {foo="bar", baz="Blech"}); }
Then just call the action method using Ajax. You could use one of the helper methods from the ViewPage such as
<%= Ajax.ActionLink("SomeActionMethod", new AjaxOptions {OnSuccess="somemethod"}) %>
SomeMethod would be a javascript method that then evaluates the Json object returned.
If you want to return a plain string, you can just use the ContentResult:
public ActionResult SomeActionMethod() { return Content("hello world!"); }
ContentResult by default returns a text/plain as its contentType.
This is overloadable so you can also do:
return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");
Related Posts:
- How to cache json with wp-super cache
- Why use wp_send_json() over echo json_encode()?
- WordPress function that makes HTML safe to be sent via AJAX request
- WP REST API route request explain
- How to use wp_send_json_error?
- Most performant way of fetching remote API data?
- json encode not work in wordpress
- Unable to parse JSON response from wp_send_json_success [closed]
- Ajax store response json in variables
- How do I query posts by a sub value with the API?
- Cache wp-json/posts without a plugin?
- merge wp rest api query to get posts per category does not work
- Retrieve JSON file from JS trough php
- Upload data from weather station to WordPress Website
- Sql formatting for post data within function
- How can I wp_send_json data?
- Full Front End, AJAX comment system and comment reply script
- Send checkbox status with Ajax / JSON and save it
- Chained ajax call, second call returns 0
- Display full WordPress post under search form using AJAX
- Can I use application/json content type in WordPress
- Using wp_send_json_success instead of $result[‘message’], die, etc
- AJAX POST Value not being returned – WordPress – AutoTrader API
- Ajax call works for logged in users and returns “Bad Request” for guests [duplicate]
- How to inject data content from external json into a modal, using UIkit?
- how to correctly use json_encode with ajax
- WorddPress website admin part not working correctly – I think ajax/json issue
- 403 error for ajax POST request
- Ajax WordPress pass post URLs
- How to populate data from JSON using AJAX in TypeScript? [closed]
- WordPress Ajax JSON success return no being recognized
- Ajax contact form returnig 0
- AJAX Call in Plugin Returns More than JSON
- Usage of wp_send_json_success and wp_redirect at the same time
- Bad Request 400… jQuery ajax post of json data to wordpress admin-ajax.php
- Google Maps API throws “Uncaught ReferenceError: google is not defined” only when using AJAX
- Best way to end WordPress ajax request and why?
- Why not register shortcodes if is_admin dashboard?
- Contact Form 7 Custom Post Action
- AJAX vs Fragment Caching for W3 Total Cache [closed]
- Detecting post type within init action
- Delete row of custom table in WordPress using AJAX
- jQuery Autocomplete in WordPress
- Metabox cloning via Admin Ajax call issue
- help on wp_editor via ajax load [duplicate]
- ajax and nonce when JavaScript is in a seperate file
- How to allow to user non logged in WP system upload in media library?
- Buddypress ajax problem after adding new groups sub nav [closed]
- How to check username availability with ajax at registration
- admin-ajax.php slow, how to speed it up?
- How to use several wp_ajax_ functions for different queries?
- I’m getting a 401 while calling the wp-json endpoint via ajax
- problem with WordPress ajax
- Ajax simple experiment
- How to make tabulator ajax call in wordpress?
- How would you update a frontend form with AJAX
- Radio buttons live refresh in the customizer
- Admin ajax add tag callback
- Stop auto load wp-admin admin-ajax.php
- wp_enqueue_script() doesn’t work with AJAX
- Ajax Request for both logged and non logged users
- AJAX call inside plugin class not triggering
- WordPress Heartbeat API cannot parse data
- Cache plugins and ajax nonce verification
- Ajax function on #publish only saves as draft – how to make it publish?
- AJAX call broken after actualization
- WP_Ajax Hook doesn’t execute this PHP code
- Ajax function not returning any results
- wp_mail doesn’t work when logged in?
- Need help with Access-Control-Allow-Origin
- Send email via ajax
- wp_ajax handler with multiple class instances
- admin-ajax.php (aborted) error when using jQuery.get
- Storing temporary data for a custom post type
- Load page HTML content through AJAX
- Not getting an ajax readystatechange on my page
- Ajax callback not work
- WordPress Select Option Load Custom Fields
- Add ajax to plugin in admin area
- Search live Ajax
- jQuery.post returns 0
- Can’t pass var from php wp_ajax into ajax script : result undefined or null
- Ajax login fails: script sets cookies, but is_user_logged_in() returns false
- admin-ajax error 400
- wordpress admin ajax trash_comment
- Ajax call on class returns old data
- delete post meta data in array WordPress
- How to trigger lost password email using REST API?
- Display post thumbnail after clicking on post id with AJAX
- Error while submitting form using AJAX and php
- How to fetch meta_value and meta_key in matrix after ajax request by post_id
- Manually cache a special, non-WP-but-using-WP page (e.g. Ajax results) with W3TC
- Is it a good idea to init ajax functions only while ‘DOING_AJAX’
- register_setting and AJAX?
- ajax response is 0 instead of ‘script’ [duplicate]
- A refreshless WordPress website
- get_template_part() does not render after the ajax request
- $wpdb Ajax not redirecting to main page
- how reduce fetch/XHR response time
- Custom WP rest api endpoint only working on non https?