0

I am new to Sitecore and .NET. I got an old existing project which is based on Sitecore 6.5 and rendered content by XSLT with .NET Framework 3.5.

Now what I need to create a page that can make an AJAX call so that the page need not to be refreshed and new content can be generated. I am quite familiar with AJAX call with PHP, yet I am quite confused on those in .NET.

I googled and found most of the tutorial are based on Razor view rendering.

Can anyone provide me a full picture that how can I do to meet my objective?

I wonder if the following steps are correct:

  1. Create a .xslt for rendering different content based on matching the URL parameter that passed into
  2. Create a .ashx to get the .xslt content
  3. JavaScript AJAX call to the .ashx and convert the xml content to HTML

Any examples that I can follow?

Thanks!

============================================

Update: I tried the above flow and can print Hello World by AJAX successfully. However, I am not sure how to get the content from XSLT in the following .ashx file with different parameter?

And are there any HttpPost/IsPostBack that can help to check if the .ashx is visited by a POST method?

enter image description here

HUNG
  • 525
  • 7
  • 17
  • You can refer [this](http://stackoverflow.com/questions/1349118/jquery-ajax-post-results-in-500-internal-server-error) question that can help you. – Jay Tankariya Aug 17 '16 at 09:23
  • Thanks Jay. I updated my question above. I can call AJAX successfully but face difficult when handling the communication between XSLT and .ashx! – HUNG Aug 17 '16 at 10:02
  • I don't think I can specifically answer your question, but it may be worth looking at the documentation for the HttpContext object to help you understand how to work with a generic handler: https://msdn.microsoft.com/en-us/library/system.web.httpcontext(v=vs.110).aspx – Brett Aug 17 '16 at 11:59

1 Answers1

0

I finally solved the question with the following steps.

  1. Create a .ashx to render the sitecore content. I have compared with .ashx, .aspx and .asmx. Seems .ashx is the best choice.

    using Sitecore.Configuration;
    using Sitecore.Data;
    using Sitecore.Data.Fields;
    using Sitecore.Data.Items;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace MYPROJECT.Web
    {
        /// <summary>
        /// Summary description for AjaxTest
        /// </summary>
        public class AjaxTest : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/html";
                string ID = context.Request.QueryString["ID"];
                Database master = Factory.GetDatabase("master");
                Item source = master.GetItem("/sitecore/content/Home/MYPROJECT/gallery");
                Item[] items = source.GetChildren().ToArray();
                context.Response.Write(items[ID].Fields["image"].Value); 
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    

p.s I removed some validation codes from above.

  1. JavaScript AJAX call to the .ashx.

        $j('#ajax_test').click(function(){
            $j.ajax({
                //type: 'POST',
                url: '/AjaxTest.ashx?ID='+$j('#inputunmber').val(),  
                //dataType: 'html',
                //data: form.find(':input').serialize(),
                success: function( response ) {
                    $j('.test_result').append(response);
                },
                error: function (xhr) {
                    alert("Error:" + xhr.responseText);
                }
            });  
        });
    
HUNG
  • 525
  • 7
  • 17