Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a custom webpart within Sharepoint and am trying to apply some ajax to the webpart.

The same code works within a .net web application but not within a sharepoint web part.

The code from the .net web application is shown below (the aspx page and the code behind) which works fine.:

ASPX File

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
 <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  // Add the page method call as an onclick handler for the div.
    $("#Result").click(function () {
        var loc = window.location.href;
    $.ajax({
      type: "POST",
      url: loc + "/GetMessage",
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(msg) {
        // Replace the div's content with the page method's return.
        $("#Result").text(msg.d);
      }
    });
  });
});

   </script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <div id="Result">Click here for the time.</div>
    <asp:Button ID="btnClick" runat="server" Text="Button" />
</asp:Content>

.CS File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace WebApplication3
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetMessage()
        {
            return "Codebehind method call...";
        }
        [WebMethod]
        public static string GetDate()
        {
            return DateTime.Now.ToString();
        }
    }
}

The text "Click here for the time" changes to "Code behind method call" when I click on it without doing a postback. I can step into the code and this calls the method GetMessage() in the code behind within a .net web application.

However this does not work on a webpart within sharepoint 2010. Does anyone have any ideas?

share|improve this question
add comment

2 Answers

Vinnie,

Did you tried making a separate Web Service for this, as mentioned in the below article:
AJAX Style Web Parts in SharePoint the Easy Way

I hope this helps

share|improve this answer
    
Hi Arsalan I will try creating a webservice and see if this fixes the issue. –  Miyagi Nov 19 '12 at 10:36
add comment

As I can not put comments, these are some things I would try:

  1. change jquery library to a newer version instead of 1.2.6
  2. implement ajax error(jqXHR, textStatus, errorThrown), and do some console log or alert message, or use firebug or developer tools to debug.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.