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 created a sandboxed solution for SharePoint 2010 which contains sandboxed visual webpart. I need to make ajax POST call and pass the entities from javascript. can you tell me how can I accomplish this requirement? How can we load the .aspx page and where will I write the code behind so that I can get the variables passed using ajax POST?

$.ajax({
 url: "https://mydomain.com/Sandbox/webservice/page.aspx",
  beforeSend: function (xhr){
         xhr.setRequestHeader('command', 'name');
        },
        type: "POST",
        dataType: "html",
        success:function(){var x=something}
          timeout: 5000
    });

Now my question is how can i load the aspx page and how can i access the entities like "command" from code behind?

share|improve this question
add comment

2 Answers 2

You put your ajax call in a separate javascript function that takes your parameters as arguments:

function myFunction(command, name){
    ...
}

Then you can call it from your web part by printing out the html to call the function with your arguments passed in. In a standard web part I did it myself like this:

protected override void RenderContents(HtmlTextWriter html)
{
    ...
    html.Write(string.Format("<script>ExecuteOrDelayUntilScriptLoaded(function () {{ myfunction('{0}','{1}') }}, 'sp.js');</script>", myCommand, myName)));
}

You could accomplish the same with a literalcontrol if you don't use the RenderContents method.

share|improve this answer
    
Do you have a working sample of the same? actually I am trying to pass the data from javascript(client) to server side using ajax call (POST) method.. The solution which you have told seems to be different from the requierment. Email ID: sharmila.k88@gmail.com –  sharmila Oct 12 '12 at 9:43
    
I seem to have misunderstood; from your first post I thought you wanted to pass a variable from your web part to your javascript. You actually want to do the opposite? –  Spongeroberto Squarepantalones Oct 12 '12 at 11:12
    
Yes. i need to pass variables from javascript to server side using ajax call –  sharmila Oct 12 '12 at 12:00
add comment

hi if you are using ajax control tool kit make sure that tool kit is compactable with the SP2010 if its compactable then your ajax codes will work. you can open a visual webpart and add your ajax codes and deploy it into your SP2010 site. then add the webpart.

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.