衫裤跑路 2013-03-12 19:00 采纳率: 50%
浏览 59

阿贾克斯不会成功

I'm trying to get data from my webapi server. Debugging shows

GET http://localhost:62578/api/DB [HTTP/1.1 200 OK 2ms]

but I dont hit success, instead I hit error and no data is passed in to the error function?

var records;
var loadedData = new Array();

$.ajax({
    type: 'GET',
    dataType: 'json',  
    accept: "application/json",
    url: 'http://localhost:62578/api/DB',
    //data: { a:'a' },
   // contentType: 'application/json; charset=utf-8',
    success: function(data) {
    //I never get here.
        $.each(data, function (key, val) {//key is the row number, val is the  object data below
                $.each(val, function (key2, val2) {//key2 is the array element name, val2 is the data. 
                    if (key == 0) loadedData.push(key2);  //we only need one row of collumns
                });
                records = key;
                init(loadedData);   
                });
            },
             error: function(jqXHR, exception) {
            //both return undefined.
        }
        });

webapi server log.

jttp = http FYI ( stupid Stack OverFlow )

iisexpress.exe Information: 0 : Request, Method=GET, Url=jttp://localhost:62578/api/DB, Message='jttp://localhost:62578/api/DB'
iisexpress.exe Information: 0 : Message='DB', Operation=DefaultHttpControllerSelector.SelectController
iisexpress.exe Information: 0 : Message='MvcApplication1.Controllers.DBController', Operation=DefaultHttpControllerActivator.Create
iisexpress.exe Information: 0 : Message='MvcApplication1.Controllers.DBController', Operation=HttpControllerDescriptor.CreateController
iisexpress.exe Information: 0 : Message='Selected action 'GetAllProducts()'', Operation=ApiControllerActionSelector.SelectAction
iisexpress.exe Information: 0 : Operation=HttpActionBinding.ExecuteBindingAsync
iisexpress.exe Information: 0 : Message='Action returned 'MvcApplication1.Models.DataBase[]'', Operation=ReflectedHttpActionDescriptor.ExecuteAsync
iisexpress.exe Information: 0 : Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance
iisexpress.exe Information: 0 : Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate
iisexpress.exe Information: 0 : Operation=ApiControllerActionInvoker.InvokeActionAsync, Status=200 (OK)
iisexpress.exe Information: 0 : Operation=DBController.ExecuteAsync, Status=200 (OK)
iisexpress.exe Information: 0 : Response, Status=200 (OK), Method=GET, Url=jttp://localhost:62578/api/DB, Message='Content-type='application/json; charset=utf-8', content-length=unknown'
iisexpress.exe Information: 0 : Operation=JsonMediaTypeFormatter.WriteToStreamAsync
iisexpress.exe Information: 0 : Operation=DBController.Dispose
  • 写回答

1条回答 默认 最新

  • weixin_33725239 2013-03-14 17:34
    关注

    I think your javascript code is located on different port; maybe port 80 and your json request is located on port 62578. Ajax request are allowed only in the same domain, protocol and port.

    In order for you to solve your issue, you need to use JSONP datatype or any other similar mechanism (server side solutions).

    Checkout wikipedia for more information about JSONP.

    Checkout this example on how to use JSONP with jQuery ajax call.

    Checkout stackoverflow for similar questions.

    评论

报告相同问题?