Is there any way to use the JQuery GetJSON method to get HTML from an external page?

前端 未结 3 626
野趣味
野趣味 2021-01-26 04:11

So let\'s say you\'re trying to do a jquery ajax request, something like:

$.ajax({
    ...
    url: http://other-website.com
    ...
})

I under

3条回答
  •  忘掉有多难
    2021-01-26 04:21

    You can retrieve any JSON object that you have access to with GetJSON. Here is an example with Razor an MVC Controller.

    jQuery Code

    $(function () {
                $.getJSON('@Url.Action("GetColorsJson", "Json")', function (jsonData) {
                    var css = new customContentJs.css.apply(jsonData);
                });
            });
    

    Controller Code

    using System.Web.Mvc;
    using DAL;
    using Newtonsoft.Json;
    
        public class JsonController : Controller
        {
            private readonly CustomContentContext _db = new CustomContentContext();
    
            /// 
            /// Return a json serialized object of user saved colors
            /// 
            /// 
            public string GetColorsJson()
            {
                return JsonConvert.SerializeObject(_db.Site.Include("Colors"));
            }
        }
    

提交回复
热议问题