zoukankan      html  css  js  c++  java
  • 此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

    此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。

    1、问题描述

    mvc从一个路径获取所有的图片信息,ajax方法如下:

    复制代码

    function getimages(day) {
                var year = $("#selYear").val();
                var month = $("#selMonth").val();
                selday = day;
                var date = year + "." + month + "." + (day < 10 ? "0" + day : day);
                $("#selInfo").text(date);
                $("#fileContainer").html("");
                $.ajax({
                    url: '@Url.Content("/MedOffice/Photo/ImageList")' + '?path=' + date,
                    type: 'GET',
                    dataType: 'text/json',
                    success: function(data) {
                        alert(data);
                    },
                    error: function(xmlHttpRequest, textStatus, errorThrown) {
                        console.log(xmlHttpRequest);
                        alert(xmlHttpRequest);
                    }
                });
            }

    复制代码

    调用执行返回如下结果:

    image

    2、问题分析

    image

    将json更改为jsonresult并设置为:

    JsonRequestBehavior = JsonRequestBehavior.AllowGet//加上这一句

    3、正确请求方法

    复制代码

    public JsonResult ImageList(string path)
            {
                List<FileInfoViewModel> files = new List<FileInfoViewModel>();
    
                try
                {
                    var datetime = DateTime.Parse(path);
                    var mypath = path.Replace(datetime.Year + ".", "");
                    var destpath = "~/AllDocuments/" + datetime.Year + "/" + mypath;
                    destpath = Server.MapPath(destpath);
                    if (Directory.Exists(destpath))
                    {
                        foreach (string filepath in Directory.GetFiles(destpath))
                        {
                            FileInfoViewModel model = new FileInfoViewModel
                            {
                                Path = filepath,
                                Name = Path.GetFileNameWithoutExtension(filepath)
                            };
                            try
                            {
                                var fileInfo = new FileInfo(filepath);
                                model.Size = fileInfo.Length.ToString();
                                model.Date = fileInfo.CreationTime.ToString("yyyy-MM-dd HH:mm:ss");
                            }
                            catch (Exception e)
                            {
                                LogHelper.Error(e);
                            }
                        }
                    }
    
                    return new JsonResult()
                    {
                        Data = new
                        {
                            result = 0,
                            messate = "",
                            data = files
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
                catch (Exception e)
                {
                    LogHelper.Error(e);
                    return new JsonResult()
                    {
                        Data = new
                        {
                            result = -99,
                            messate = e.Message,
                            data = files
                        },
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
                }
            }

    复制代码

  • 相关阅读:
    itoa
    sprintf用法 [转载]
    atoi 与 itoa的实现
    数组排序总结(冒泡,选择,插入,希尔)
    XML入门精解之文件格式定义(DTD)
    malloc()函数的工作机制 结构体的总结
    字符串指针与字符数组(ZT)
    sprintf函数你了解有多深!
    sprintf
    sql ldr 笔记
  • 原文地址:https://www.cnblogs.com/grj001/p/12225235.html
Copyright © 2011-2022 走看看