Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 处理电话间隙中的跨域呼叫_Jquery_Cordova_Cross Domain_Jsonp_Asmx - Fatal编程技术网

Jquery 处理电话间隙中的跨域呼叫

Jquery 处理电话间隙中的跨域呼叫,jquery,cordova,cross-domain,jsonp,asmx,Jquery,Cordova,Cross Domain,Jsonp,Asmx,我正在寻找以下方面的最佳实践实施 我有一个调用.asmx web服务的JQuery脚本,但在域外调用时无法加载,我知道当我将html移到PhoneGap时会发生这种情况 使用JSONP是最佳/唯一的解决方案吗 我不喜欢.asmx或jquery等。欢迎您使用过的任何行之有效的想法 只需要能够从html5在服务器上调用方法。(html5应用程序不会离线运行) 谢谢 是的,JSONP是您问题的答案。JSONP或“带填充的JSON”是一个JSON扩展,其中一个前缀被指定为调用本身的一个输入参数,它只是克

我正在寻找以下方面的最佳实践实施

我有一个调用.asmx web服务的JQuery脚本,但在域外调用时无法加载,我知道当我将html移到PhoneGap时会发生这种情况

使用JSONP是最佳/唯一的解决方案吗

我不喜欢.asmx或jquery等。欢迎您使用过的任何行之有效的想法

只需要能够从html5在服务器上调用方法。(html5应用程序不会离线运行)


谢谢

是的,JSONP是您问题的答案。JSONP或“带填充的JSON”是一个JSON扩展,其中一个前缀被指定为调用本身的一个输入参数,它只是克服XMLHttpRequest相同域策略的一个技巧

,


这两个链接应该可以帮助您。如果您有任何问题,请告诉我。

要了解有关JSONP的更多信息,请单击

简单地说,JSONP可以定义为(这是Remy Sharp博客的摘录)

JSONP是脚本标记注入,将来自服务器的响应以 到用户指定的函数


这也可以帮助你找到解决问题的方法

如果使用Phonegap,它将使用file://协议加载本地html文件。加载失败可能与有关,而这不适用于文件://协议。因此,使用file://协议加载html,您可以进行跨浏览器调用。查看以了解更多信息。也检查一下


祝你好运

我要说的是,JSONP绝对是最好的方法,如果您需要在服务器端代码中考虑
选项
方法,您可以实现类似的代码。(注意:本示例在技术上适用于MVC,但可以修改。)

旁注:这是我正在使用的JsonpResult。真是太棒了

namespace System.Web.Mvc
{
#区域使用
使用制度;
使用WebService.Attributes;
使用WebService.Domain.Models;
使用WebService.Exceptions;
使用WebService.Models.ViewModels;
使用扩展;
使用WebService.Utilities;
#端区
公共类JsonpResult:ActionResult
{
公共对象数据{get;set;}
公共字符串JsonCallback{get;set;}
公共布尔?成功{get;set;}
公共覆盖无效ExecuteSult(ControllerContext上下文)
{
//创建一个我们可以发送的JsonResponse
//检查回调参数
this.JsonCallback=context.HttpContext.Request[“callback”];
//如果“回调”不存在,我们要检查“jsoncallback”
if(string.IsNullOrEmpty(this.JsonCallback))
this.JsonCallback=context.HttpContext.Request[“JsonCallback”];
//如果我们做到了这一点,我们就知道发送的对象是
//对象,我们可以将其序列化并作为有效的JSON发送回
//需要将其包装在我们的JsonViewModel中以保持一致性
//使用AsList()扩展方法确保
//由于我们的应用程序(Sencha Touch)需要
//所有数据都将采用数组格式。
如果(!Data.GetType().IsGenericType)
Data=Data.AsList();
//到目前为止,我们发现数据要么是错误的,要么是空的
//如果为null,并且数据不为null,则将Success=true
//如果成功是假的,那是因为我们在抛出错误时将其设置为false
//如果是这样的话,我们为客户保留虚假信息。
如果(Success==null)Success=true;
var jsonViewModel=新的jsonViewModel
{
结果=此数据,
//我们知道成功不是真是假,所以我们可以
//安全地将可为空的布尔值转换为布尔值。
success=(bool)this.success
};
写出(jsonViewModel,上下文);
}
/// 
///向控制器写出JsonP响应。
/// 
///未序列化对象
///控制器上下文
私有无效写出(JsonViewModel jsonResponse,ControllerContext上下文)
{
var response=context.HttpContext.response;
response.ContentType=“应用程序/javascript”;
Write(JsonUtility.Serialize(jsonResponse,this.JsonCallback));
}
}
/// 
///控制器允许jsonp的扩展方法。
/// 
公共静态类ContollerExtensions
{
公共静态JSONPRESSLT Jsonp(此控制器、对象数据)
{
返回新的JsonpResult{Data=Data};
}
公共静态JsonpResult Jsonp(此控制器、对象数据、bool success)
{
//我们永远不想错过“真正”的成功,我们只想
//知道它是否失败了。
如果(成功)
{
返回新的JsonpResult{Data=Data};
}
返回新的JsonpResult{Success=false,Data=Data};
}
}
}

听起来很有趣。但是你的第三个链接不起作用
public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Cache.SetNoStore();

        filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");

        string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"];
        if (rqstMethod == "OPTIONS" || rqstMethod == "POST")
        {
            filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
            filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept, Access-Control-Allow-Origin, Content-Type");
        }
        base.OnActionExecuting(filterContext);
    }
}
[HttpGet]
[AllowCrossSiteJsonAttribute]
public JsonpResult CommunicateCard(CommunicateCardModel ccm)
{
    return ModelState.IsValid
        ? this.ValidCommunicateCardBuilder(ccm)
        : this.InvalidFormSummary(ccm);
}
namespace System.Web.Mvc
{
    #region usings
    using System;

    using WebService.Attributes;
    using WebService.Domain.Models;
    using WebService.Exceptions;
    using WebService.Models.ViewModels;
    using Extensions;
    using WebService.Utilities;
    #endregion

    public class JsonpResult : ActionResult
    {
        public Object Data { get; set; }
        public string JsonCallback { get; set; }
        public bool? Success { get; set; }


        public override void ExecuteResult(ControllerContext context)
        {

            // Create a JsonResponse that we can Send out

            // Check for the callback parameter
            this.JsonCallback = context.HttpContext.Request["callback"];

            // if the "callback" doesn't exist, we want to check for "jsoncallback"
            if (string.IsNullOrEmpty(this.JsonCallback))
                this.JsonCallback = context.HttpContext.Request["jsoncallback"];

            // If we've made it this far, we know that the object sent is an
            // object that we can serialize and Send back as valid JSON. We now
            // need to wrap it in our JsonViewModel for consistancy. NOTE: We're
            // using the AsList() extension method to ensure that we're
            // returning an Array since our application (Sencha Touch) requires
            // all data to be in array format.
            if (!Data.GetType().IsGenericType)
                Data = Data.AsList();

            // Until now we the data is either an ERROR, or null
            // if it's null, and the Data is not null, we set Success = true
            // If Success is false, it's because we set it to false when we threw an error
            // if that's the case, we keep it false for the client.
            if (Success == null) Success = true;


            var jsonViewModel = new JsonViewModel
                {
                    results = this.Data,
                    // We know that Success is either True or False so we can
                    // safely cast the nullable bool to a bool.
                    success = (bool)this.Success
                };
            WriteOut(jsonViewModel, context);
        }

        /// <summary>
        /// Write Out the JsonP Response to the Controller.
        /// </summary>
        /// <param name="jsonResponse">The unserialized object</param>
        /// <param name="context">Controller Context</param>
        private void WriteOut(JsonViewModel jsonResponse, ControllerContext context)
        {
            var response = context.HttpContext.Response;
            response.ContentType = "application/javascript";
            response.Write(JsonUtility.Serialize(jsonResponse, this.JsonCallback));
        }
    }

    /// <summary>
    /// extension methods for the controller to allow jsonp.
    /// </summary>
    public static class ContollerExtensions
    {
        public static JsonpResult Jsonp(this Controller controller, Object data)
        {
            return new JsonpResult { Data = data };
        }

        public static JsonpResult Jsonp(this Controller controller, Object data, bool success)
        {
            // We don't ever want to pass in a "true" success. We only want to 
            // know if it failed.
            if (success)
            {
                return new JsonpResult { Data = data };
            }

            return new JsonpResult { Success = false, Data = data };
        }
    }
}