Calling ASP.NET Web API using JQuery ajax - cross site scripting issue

Posted by SimonF on Stack Overflow See other posts from Stack Overflow or by SimonF
Published on 2012-11-03T08:36:42Z Indexed on 2012/11/03 11:01 UTC
Read the original article Hit count: 343

Filed under:
|
|
|

I have a Web API which I am calling using the JQuery ajax function. When I test the service directly (using the Chrome RESTEasy extension) it works fine, however when I call it using the JQuery ajax function I get an error. I'm calling it on port 81:

$.ajax({
   url: "http://127.0.0.1:81/api/people",
  data: JSON.stringify(personToCreate),
  type: "POST",
  contentType: "application/json;charset=utf-8",
  statusCode: {
     201: function (newPerson) {
        callback(newPerson);
     }
  },
  success: function (newPerson) {
     alert("New person created with an Id of " + newPerson.Id);
  },
  error: function (jqXHR, textStatus, errorThrown) {
     alert('Error. '+textStatus+'. '+errorThrown);
  }
});

...but when I trace it using FireBug Lite the response comes from port 82:

{"Message":"No HTTP resource was found that matches the request URI 'http://127.0.0.1:82/api/people'.","MessageDetail":"No action was found on the controller 'People' that matches the request."}

I think the error is, effectively, due to cross-site scripting being blocked, but I'm not actually cross-site scripting, if you see what I mean.

Has anyone else come across this and been able to fix it?

Edit: Routing config (global.asax.vb) is:

RouteTable.Routes.MapHttpRoute(name:="DefaultApi", routeTemplate:="api/{controller}/{id}", defaults:=New With {Key .id = System.Web.Http.RouteParameter.Optional})

Controller:

Public Function PostValue(ByVal departmentid As Integer, ByVal emailaddress As String, ByVal firstname As String, ByVal lastname As String) As Guid
    Dim context As New WSMModelDataContext
    Dim bllPeople As New PeopleBLL(context)

    Return bllPeople.Create(firstname, lastname, emailaddress, departmentid)
End Function

When I debug it, it doesn't get as far as running the controller, although when calling it through RESTEasy it routes correctly and the controller executes successfully. The only difference seemes to be that wen called through RESTEasy it is (correctly) using

http://127.0.0.1:81 

but for some reason when called via JQuery/ajax it seems to be using

http://127.0.0.1:82.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about ASP.NET