关于jsonp的问题

时间:2022-12-18 09:14:41
我现在在做一个项目,遇到了跨域ajax的问题。

所以网页里面用到了$.getJSON(),向本地Webservice请求;

本地的Webservice是我自己写的,用来响应$.getJSON();

我用$.getscript()测试了一下webservice是可以正常工作的。但是$.getJSON()总是不成功。

我怀疑是不是我返回的流不对,各位有熟悉的帮忙看一下,谢谢了;

http头是这样:

public void SendHeader(string sHttpVersion, string sMIMEHeader, int iTotBytes, string sStatusCode, ref Socket mySocket)
        {
            string sBuffer = "";
            if (sMIMEHeader.Length == 0)
            {
                sMIMEHeader = "text/html";
            }

            sBuffer = sBuffer + sHttpVersion + sStatusCode + "\r\n";
            sBuffer = sBuffer + "Server:cx1193719-b\r\n";
            sBuffer = sBuffer + "Content-Type:" + sMIMEHeader + "\r\n";
            sBuffer = sBuffer + "Accept-Ranger:bytes\r\n";
            sBuffer = sBuffer + "Content-Length:" + iTotBytes + "\r\n\r\n";
            byte[] bSendData = Encoding.ASCII.GetBytes(sBuffer);
            SendToBrowser(bSendData,ref mySocket);
            LogToListBox("总字节:" + iTotBytes.ToString());
        }

2 个解决方案

#1


jsonp和getscript不一样,jsonp动态页需要接收回调参数名称并输出

jQuery jsonp跨域原理

#2


我的例子:
前端js:

                            $.getJSON(
                                '/PnrExist?Pnr=HQTE11&jsonpcallback=?',
                                function (res) {
                                    var data = res.Data;
                                    if (data != null && data.memo != null) {
                                        if (data) {
                                             alert(data.memo);
                                        }
                                        else
                                            alert('获取失败!');
                                    }
                                }
                            );



webservice我用wcf实现:


    [JavascriptCallbackBehavior(UrlParameterName = "jsonpcallback")]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PlatformService
    {
        /// <summary>
        /// Pnr是否存在
        /// </summary>
        /// <param name="Pnr">要查询的Pnr</param>
        /// <returns>是/否已存在</returns>
        private bool PnrExist(string Pnr)
        {
            return PlatformOutMgr.GetInstance().PnrExist(Pnr);
        }
    }

#1


jsonp和getscript不一样,jsonp动态页需要接收回调参数名称并输出

jQuery jsonp跨域原理

#2


我的例子:
前端js:

                            $.getJSON(
                                '/PnrExist?Pnr=HQTE11&jsonpcallback=?',
                                function (res) {
                                    var data = res.Data;
                                    if (data != null && data.memo != null) {
                                        if (data) {
                                             alert(data.memo);
                                        }
                                        else
                                            alert('获取失败!');
                                    }
                                }
                            );



webservice我用wcf实现:


    [JavascriptCallbackBehavior(UrlParameterName = "jsonpcallback")]
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PlatformService
    {
        /// <summary>
        /// Pnr是否存在
        /// </summary>
        /// <param name="Pnr">要查询的Pnr</param>
        /// <returns>是/否已存在</returns>
        private bool PnrExist(string Pnr)
        {
            return PlatformOutMgr.GetInstance().PnrExist(Pnr);
        }
    }