0

この記事を読みました: http://bloggingabout.net/blogs/adelkhalil/archive/2009/08/14/cross-domain-jsonp-with-jquery-call-step-by-step-guide.aspx

次のようにjsのコードであるjquery ajaxでWebサービスを呼び出そうとして います:

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);

}

Web サービス: 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 は呼び出されませんでした

textStatus: 「パーサーエラー」

jqXHR: readyState = 4、ステータス = 200、statusText = "成功"

アイデア?どうすれば解決できますか?? ありがとう!

4

0 に答える 0