0

我读了这篇文章:http ://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx

我尝试通过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);

}

网络服务:Global.asax:

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 方法的调用按预期返回。

对于远程机器,我收到一个错误:

errorThrown: jQuery110107575608874517119_1374744381047 未被调用

文本状态:“解析器错误”

jqXHR: readyState = 4, status = 200, statusText = "success"

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

4

0 回答 0