0

I'm trying to receive some json from my asp.net mvc webservice. ASP.NET:

    public JsonResult getMessagesFromFriend(string email, string friendmail) {
        DataLayer.Repository.Classes.DataLayer dl = DataLayer.Repository.Classes.DataLayer.Instance;
        return Json(dl.getMessagesFromFriend(email, friendmail);
    }

When i send a test post message with http analyzer i get:

    Tue May 01 11:24:59 CEST 2012
    ----------------------------------------
    POST http://xxx.xxx.xxx.xxx/tracy/chat/getMessagesFromFriend?email=linsy&friendmail=djcarre
    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Type: application/json; charset=utf-8
    Server: Microsoft-IIS/7.5
    X-AspNetMvc-Version: 4.0
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Tue, 01 May 2012 09:25:04 GMT
    Content-Length: 309

    ----------------------------------------
    ----------------------------------------
    [{"sender":"viagsm","receiver":"djcarre","datetime":"28/04/2012 16:55:33","message":"msg1"},{"sender":"viagsm    ","receiver":"djcarre","datetime":"28/04/2012 16:55:59","message":"msg2"},{"sender":"Philippe C","receiver":"linsy","datetime":"28/04/2012 16:56:10","message":"test3"}]
    ----------------------------------------

So it's returning my json. My javascript code looks like this:

    $.post("http://xxx.xxx.xxx.xxx/tracy/chat/getMessagesFromFriend?email=linsy&friendmail=djcarre", function(data) {
     if (data.length > 0) {
                  for (x in data) {
                      $("#chatarea").html(
                      $("#chatarea").html() +
                      "<p><b>" +
                      data[x].sender + "</b>" + 
                      " (" + data[x].datetime.match(/(\d+:\d+:\d+)/)[1] + ") : " + 
                      data[x].message + "</p>");
                  }
              }
    }, "json");

}

When i look in firebug, the post is sent, i get an 200 ok, but answer stays empty. I also tried with $.ajax and

    error:function(jqXHR, textStatus, errorThrown){
        alert(errorThrown);
    }

But i only get an empty alert, so there is an error, but i have no idea which one and how to solve it. I hope someone knows how to help me.

Kind regards

user1245233
  • 45
  • 1
  • 5

4 Answers4

1

You seem to have specified an absolute address for your AJAX request:

http://xxx.xxx.xxx.xxx/tracy/chat/getMessagesFromFriend?email=linsy&friendmail=djcarre

Due to the same origin policy restriction that's built into browsers you cannot send cross domain AJAX requests. There are a couple of workarounds depending on the level of control you have over the remote domain.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
0

$.post(url, para, successFunction, ErrorFunction);
you forgot to put the para paramter into the function. so it doesn't work.

Tim Li
  • 215
  • 1
  • 2
  • 8
0

Yes as mentioned by Darin you should take care of origin policy and so your ajax call should be on same domain and sub-domain from which you're serving the page in which you have your javascript written.

Another way to trace are: - Check whether your ajax call is reaching to server or not? For that you can either update some table in db or just put a file with POST data content on your local drive and dump as a file with timestamp. If this step failes it means that your request is not reaching up to server at all and so you should see whether it's cross-site ajax problem and for that you need to fix it by putting a file on the same server from which you're serving your page and then calling the actual service/page-content via your server using ASP.NET.

  • Also make sure you dump your output whatever you're sending if previous check passes and see what you're sending.

This should help, good luck.

deej
  • 2,536
  • 4
  • 29
  • 51
0

After a lot of searching i found this: Ajax json post to Controller across domains, "not allowed by" Access-Control-Allow-Headers It provided a solution for my problem.

Because my script was running on a mobile phone and the webservice on a remote server i had to use jsonp. Thanks for the help !

Community
  • 1
  • 1
user1245233
  • 45
  • 1
  • 5