JSON返回错误与ASP [英] JSON return error with ASP

查看:159
本文介绍了JSON返回错误与ASP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用由外部供应商编写的ASP应用程序.我的任务是对应用程序进行一些小的更改,但是我对ASP或JSON一无所知.通过一些研究,我将其组合在一起.我在表单上创建了一个文本框,我想将客户端IP地址返回到该文本框.我写了一个函数,然后是一个控制器.两者的代码如下:

We are using a ASP app written by an outside vendor. I am tasked with making a small change to the app however I don't know anything about asp or json. Through some research I have put this together. I created a text box on the form and I want to return the client IP address to that text box. I wrote a function then a controller. The code for both is below:

功能

function processgetip(event) {
    // Within this function, make an AJAX call to get the IP Address
    $.getJSON('@Url.Action("GetIPAddress","getipaddress")', function (ip) {
        // When this call is done, your IP should be stored in 'ip', so
        // You can use it how you would like

        // Example: Setting a TextBox with ID "YourElement" to your returned IP Address
        $("#facility").val(ip);
    });
}

控制器

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web;
    using System.Web.Mvc;

    namespace Parker_Hannifin.Controllers
    {
    public class getipaddressController : ApiController
{
    public JsonResult GetIPAddress()
    {

        System.Web.HttpContext context = System.Web.HttpContext.Current;

        string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (!string.IsNullOrEmpty(ipAddress))
        {
            string[] addresses = ipAddress.Split(',');
            if (addresses.Length != 0)
            {
                //return addresses[0]; //
                ipAddress = addresses[0];
            }
        }

        //replace ipaddress with ipAddress
        return Json(ipAddress, JsonRequestBehavior.AllowGet);
    }


}
    }

我在以下代码行中遇到这些错误:

I am getting these errors on this line of code:

return Json(ipAddress, JsonRequestBehavior.AllowGet);

我得到的错误是:

与之匹配的最佳重载方法 System.Web.Http.ApiController.Json(string, Newtonsoft.Json.JsonSerializerSettings)有一些无效的参数. 无法从System.Web.Mvc.JsonRequestBehavior转换为 Newtonsoft.Json.JsonSerializerSettings

The best overloaded method match for System.Web.Http.ApiController.Json(string, Newtonsoft.Json.JsonSerializerSettings) has some invalid arguments. Cannot convert from System.Web.Mvc.JsonRequestBehavior to Newtonsoft.Json.JsonSerializerSettings

如果有人可以告诉我他们的意思和解决方法,我将不胜感激.

If someone could please tell me what they mean and how to fix them I would greatly appreciate it.

推荐答案

Json具有两个参数,

protected internal JsonResult<T> Json<T>(
T content,
JsonSerializerSettings serializerSettings
)

具有两个参数的Controller中的

Json的签名为

Json in Controller with two parameters has a signature of,

protected internal JsonResult Json(
object data,
JsonRequestBehavior behavior
)

getipaddressController继承自ApiController,但是您使用了控制器方法Json.使用

getipaddressController inherited from ApiController, but you used Controller method Json. Use,

return new JsonResult()
{
 Data = ipAddress,
 JsonRequestBehavior = JsonRequestBehavior.AllowGet
};

如果您仍然想要该行为.

If you still want the behavior.

这篇关于JSON返回错误与ASP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆