调用jsonp跨域webservice时出错

时间:2014-08-14 16:55:47

标签: jquery cordova jsonp

我是一名.NET开发人员,试图使用PhoneGAP创建一个简单的跨平台(Android / iOS)应用程序。我在PhoneGAP应用程序中使用我的.NET Web服务时遇到问题,这是我的JS代码:

    function DoIt()
{
    var surl = "http://www.silverlight4web.com/onlinegame/rest.aspx";
    $.ajax({
        type: 'GET',
        url: surl,
        crossDomain: true,
        contentType: "application/javascript",
        data: { "UserID": "1234" },
        dataType: "jsonp",
        success: function(val) {
            var myjsonobject = val;
            alert(myjsonobject.accounting[0].firstname + ' ' + myjsonobject.accounting[0].lastname);
        },
        error: function(xhr, status, error) { alert(error); },
        async: false,
        cache: false
    });

}
DoIt();

这是我的服务器端页面(CS):

        public string output = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["callback"] != null)
        {
            output = Request.Params["callback"] + "(" + GetData() + ");";
        }
    }

    public string GetData()
    {
        string r = @"
            { ""accounting"" : [   // accounting is an array in employees.
                                { ""firstName"" : ""John"",  // First element
                                  ""lastName""  : ""Doe"",
                                  ""age""       : 23 },

                                { ""firstName"" : ""Mary"",  // Second Element
                                  ""lastName""  : ""Smith"",
                                  ""age""       : 32 }
                              ], // End ""accounting"" array.                                 
              ""sales""       : [ // Sales is another array in employees.
                                { ""firstName"" : ""Sally"", // First Element
                                  ""lastName""  : ""Green"",
                                  ""age""       : 27 },

                                { ""firstName"" : ""Jim"",   // Second Element
                                  ""lastName""  : ""Galley"",
                                  ""age""       : 41 }
                              ] // End ""sales"" Array.
            }";

        return r;
    }

(VB):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Rest.aspx.cs" Inherits="OnlineG.Rest" %>
<%=output %>   
..... HTML page

              

</div>
</form>

我收到以下错误:

Error: jQuery182046418637151657827_1408038693745 was not called

每次JQuery前面的数字都会发生变化,我在google中看到过这个问题的一些实例但没有一个帖子可以解决我的问题,出了什么问题?我应该检查/更改服务器上的任何内容或者缺少某些代码吗?

1 个答案:

答案 0 :(得分:1)

我之前遇到类似的问题(服务器端我使用了node.js),不记得我是如何完全修复它的。我没有环境来测试你的情况。但是这里有一些提示可以解决它们,

  1. 您需要在网址中传递回调函数名称,所以它应该是, var surl =“http://www.silverlight4web.com/onlinegame/rest.aspx?callback=success”,

  2. 在页面中编写一个单独的js函数来执行json数据处理,例如,

     
    function success(data) {
    //your code
    var myjsonobject = data;
    alert(myjsonobject.accounting[0].firstname + ' ' + myjsonobject.accounting[0].lastname);
    }
    function DoIt()
    {
    var surl = "http://www.silverlight4web.com/onlinegame/rest.aspx";
    $.ajax({
        type: 'GET',
        url: surl,
        crossDomain: true,
        contentType: "application/javascript",
        data: { "UserID": "1234" },
        dataType: "jsonp",
        success: function(val) {
            success(val);
        },
        error: function(xhr, status, error) { alert(error); },
        async: false,
        cache: false
    });
    
    }
    DoIt();
    
  3. 确保服务器端最终返回“success(您的json数据);”,如果以下代码不起作用,则必须使用单独的web api / service来执行此操作;

     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["callback"] != null)
        {
            //here will return "success(...)"
            output = Request.Params["callback"] + "(" + GetData() + ");";
        }
    }
    
  4. 您可能需要在服务器端指定输出内容类型,例如, ... Response.ContentType =“application / javascript”;
  5. 让我知道它是否有效:)

相关问题