Jquery 远程机器的ajax问题-跨域问题

Jquery 远程机器的ajax问题-跨域问题,jquery,ajax,web-services,Jquery,Ajax,Web Services,我读了这篇文章: 我试图通过jquery ajax调用web服务, js中的代码如下: function AjaxRequests(data, method, isAsync, onSuccessCall, onComplete) { jQuery.support.cors = true; var localAjax = { contentType: "application/json; charset=utf-8;", url: webService + "/" + meth

我读了这篇文章:

我试图通过jquery ajax调用web服务, js中的代码如下:

function AjaxRequests(data, method, isAsync, onSuccessCall, onComplete) {
jQuery.support.cors = true; 
var localAjax = {
    contentType: "application/json; charset=utf-8;",
    url: webService + "/" + method,
    data: data,
    dataType: "jsonp",
    async: isAsync,
    crossDomain: true,
    type: "get"
};
requestAjax = $.ajax(localAjax);
requestAjax.done(onSuccessCall);
requestAjax.fail(OnError);
requestAjax.complete(onComplete);
const string JSON_CONTENT_TYPE = "application/json; charset=utf-8;";
const string ASMX_FILE = ".asmx";
const string ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
const string STAR_ALL = "*";

public override void Init()
{
    base.Init();
    this.ReleaseRequestState += Global_ReleaseRequestState;
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.Url.AbsolutePath.Contains(ASMX_FILE))
    {
        return;
    }
    HttpContext.Current.Response.AddHeader(ACCESS_CONTROL_ALLOW_ORIGIN, STAR_ALL);
    if (string.IsNullOrEmpty(HttpContext.Current.Request.ContentType))
    {
        HttpContext.Current.Request.ContentType = JSON_CONTENT_TYPE;
    }
}

void Global_ReleaseRequestState(object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;
    HttpResponse response = app.Response;
    if (app.Context.Request.ContentType != JSON_CONTENT_TYPE)
    {
        return;
    }
    response.Filter = new JsonResponseFilter(response.Filter);
}

 protected void Application_End(object sender, EventArgs e)
{
    this.ReleaseRequestState -= Global_ReleaseRequestState;
}
}

Web服务: Global.asax:

function AjaxRequests(data, method, isAsync, onSuccessCall, onComplete) {
jQuery.support.cors = true; 
var localAjax = {
    contentType: "application/json; charset=utf-8;",
    url: webService + "/" + method,
    data: data,
    dataType: "jsonp",
    async: isAsync,
    crossDomain: true,
    type: "get"
};
requestAjax = $.ajax(localAjax);
requestAjax.done(onSuccessCall);
requestAjax.fail(OnError);
requestAjax.complete(onComplete);
const string JSON_CONTENT_TYPE = "application/json; charset=utf-8;";
const string ASMX_FILE = ".asmx";
const string ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";
const string STAR_ALL = "*";

public override void Init()
{
    base.Init();
    this.ReleaseRequestState += Global_ReleaseRequestState;
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
    if (!HttpContext.Current.Request.Url.AbsolutePath.Contains(ASMX_FILE))
    {
        return;
    }
    HttpContext.Current.Response.AddHeader(ACCESS_CONTROL_ALLOW_ORIGIN, STAR_ALL);
    if (string.IsNullOrEmpty(HttpContext.Current.Request.ContentType))
    {
        HttpContext.Current.Request.ContentType = JSON_CONTENT_TYPE;
    }
}

void Global_ReleaseRequestState(object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;
    HttpResponse response = app.Response;
    if (app.Context.Request.ContentType != JSON_CONTENT_TYPE)
    {
        return;
    }
    response.Filter = new JsonResponseFilter(response.Filter);
}

 protected void Application_End(object sender, EventArgs e)
{
    this.ReleaseRequestState -= Global_ReleaseRequestState;
}
JsonResponseFilter类:

 public class JsonResponseFilter : Stream
    {
        private const string CALLBACK = "callback";
        private const string RETURN_REQUEST = "{0}({1}";
        private const string RIGHT_BRACKET = ");";
        private readonly Stream _responseStream;
        private long _position;
        private bool _first = true;
        public JsonResponseFilter(Stream responseStream)
        {
            _responseStream = responseStream;
        }

        public override bool CanRead { get { return true; } }

        public override bool CanSeek { get { return true; } }

        public override bool CanWrite { get { return true; } }

        public override long Length { get { return 0; } }

        public override long Position { get { return _position; } set { _position = value; } }

        public override void Write(byte[] buffer, int offset, int count)
        {
            string strBuffer = Encoding.UTF8.GetString(buffer, offset, count);
            strBuffer = AppendJsonpCallback(strBuffer, HttpContext.Current.Request);
            byte[] data = Encoding.UTF8.GetBytes(strBuffer);
            _responseStream.Write(data, 0, data.Length);
        }

        private string AppendJsonpCallback(string strBuffer, HttpRequest request)
        {
            if (_first)
            {
                _first = false;
                return string.Format(RETURN_REQUEST, request.Params[CALLBACK], strBuffer);
            }
            else
            {
                return strBuffer;
            }            
        }

        public override void Close()
        {
            _responseStream.Close();
        }

        public override void Flush()
        {
            byte[] data = Encoding.UTF8.GetBytes(RIGHT_BRACKET);
            _responseStream.Write(data, 0, data.Length);
            _responseStream.Flush();
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return _responseStream.Seek(offset, origin);
        }

        public override void SetLength(long length)
        {
            _responseStream.SetLength(length);
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return _responseStream.Read(buffer, offset, count);
        }
    }
在本地计算机上,对ajax方法的调用将按预期返回

对于远程机械i,出现错误:

错误抛出:未调用JQuery10107575608874517119_1374744381047

textStatus:“parsererror”

jqXHR:readyState=4,status=200,statusText=“success”

想法?我该如何解决??
谢谢

原因可能不多。一种可能是服务器页面正在以非JSON的格式打印输出。第二个是服务器响应超时,第三个我能猜到的是根本没有输出。试着直接访问那个路径,看看你们得到了什么。那个么,对于本地机器来说,它是如何工作良好的呢?