I have been working on a JQuery script that posts JSON to my asp.net web service. If the script is on the same domain it processes correctly but when I try to run the script from a local file or on an alternative domain not appears to happen to the eye!.. Though what I have noticed is that within my web logs the attempt is reaching the server but with the method OPTIONS rather than POST.
I have tried to set the following headers within IIS, web.config directly and within my global.asax file to on the application_beginRequest event but no success thus far!
Access-Control-Allow-Origin *
Access-Control-Allow-Methods GET, POST.
My global.asax file contain this:
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDmainAjaxCall();
}
private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"GET, POST, OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
"1728000");
HttpContext.Current.Response.End();
}
}
My client side script is as follows:
$.ajax({
type: "POST",
crossDomain: true,
url: "http://myservice.mydomain.com/Data.asmx/Import",
data: "{\"AppSession\":" + JSON.stringify(session) + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
// Insert the returned HTML into the <div>.
$('#content').html(data.d);
},
error: function (j, t, e) {
$('#content').html(e);
}
});
My web service code is as follows…
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
/// <summary>
/// Summary description for DataMgt
/// </summary>
[WebService(Namespace = "DataManagement")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class DataMgt : System.Web.Services.WebService {
public DataMgt () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
public class GeoData
{
public string GEOLongitude { get; set; }
public string GEOLatitude { get; set; }
public string GEOAccuracy { get; set; }
public string GEOStatusID { get; set; }
}
public class AppSession
{
public GeoData GEOData { get; set; }
public string UserID { get; set; }
public string CountryID { get; set; }
}
[WebMethod]
public string ImportData(AppSession AppSession)
{
string strStatusCode;
try
{
//Do stuff here with requested data
strStatusCode = "SUCCESS";
}
catch (Exception ex)
{
strStatusCode = "FAILURE";
}
return strStatusCode;
}
}
As I said earlier, this runs absolutely fine when the client side script is run on the same domain.
The website that hosts my service is on IIS 7 and ASP.NET 4.
Has anyone expereinced this when using a similar setup to me or can anyone suggest how to resolve this?
Many thanks in advance,
Rit
If you working on diffrent domain you will not be able to POST request to your server. Its restricted, you have to use JSONP
JSONP:
http://api.jquery.com/jQuery.getJSON/
A tutorial:
http://abstractform.wordpress.com/2009/10/12/accessing-remote-asp-net-web-services-using-jsonp/