I have ASP.NET web app that call some WebMethod from javascipt and fills textboxes. This is not all code, just example codesnippet
function dataApp(text) {
var loc = window.location.href;
$.ajax({
type: "POST",
url: loc + "/Recount",
data: JSON.stringify({ 'data': text }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
error: function (xhr, ajaxOptions, thrownError) {
$('#mycontrol').val("bla");
$('#mycontrol2').val("bla");
// ..
}
});
}
// here we get result from server and set some values in web UI
function OnSuccess(response) {
var obj = JSON.parse(response.d);
$('#mycontrol1').val(obj.a);
$('#mycontrol2').val(obj.b);
}
MyControl.ascx.cs:
[WebMethod]
public static string Recount(string data)
{
// ..
return JsonString;
}
And it works good while Web Application Project, but now I want to migrate this code to Visual SP web part. And there is a problem connection to web method, in JS console I see(webpart added on default.aspx page):
POST http://server/web/default.aspx/Recount 500 (Internal Server Error)
How can I call my WebMethod from JS in Visual Webpart correctly?
The webpart is fully-featured, not sandboxed.
upd: user asked me an example of ascx file with webmethod, so I put it here:
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CurrencyRate.Layouts.CurrencyRate
{
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Xml;
public partial class CurrencyRate : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// Update ratings from Yahoo API
/// </summary>
[WebMethod]
public static string RefreshRates(string value)
{
try
{
// ..
return new JavaScriptSerializer().Serialize(
new
{
error = false,
// some values here
});
}
catch (Exception ex)
{
// ..
return new JavaScriptSerializer().Serialize(
new
{
error = true,
// values
});
}
}
}
}