Showing posts with label method. Show all posts
Showing posts with label method. Show all posts

Saturday, March 24, 2012

InvalidOperationException : Request format is invalid : application/json charset=utf - 8

Can anybody help me guys with this one,

I'm tryng to invoke a web method in my web service from javascript but i keep on getting javascript response Error stating that my webmethod couldn't execute because of this Error (InvalidOperationException : Request format is invalid : application/json charset=utf - 8 )

here is my logic for my web service

[ScriptService]
[WebService(Namespace = "http://www.wipspace.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class svrCreateReal : System.Web.Services.WebService
{
private clsLibraryFile objF;
private clsWipReel objWipR;


public svrCreateReal ()
{
objF = new clsLibraryFile();
objWipR = new clsWipReel();
}

[WebMethod]
[ScriptMethod(UseHttpGet = false)]
public int createReelfileCollection(string reelID, string menuID, string fileID )
{
try
{
objWipR.reelID = Convert.ToInt32(Server.HtmlEncode(reelID));
objWipR.reelMenuID = Convert.ToInt32(Server.HtmlEncode(menuID));
objF.fileID = Convert.ToInt32(Server.HtmlEncode(fileID));
return objWipR.createReelMovieCollection(objWipR, objF);
}
catch
{
return -1;
}

}

From my aspx page i call a javascript function that invokes the web method like below

<asp:ScriptManager ID="scriptManager"
runat="server" >
<Services>
<asp:ServiceReference InlineScript="true" Path="adminWipService/svrCreateReal.asmx" />
</Services>
</asp:ScriptManager>

<script type="text/javascript">

function createReel( sender, e )
{
var container = e.get_container();
var item = e.get_droppedItem();
var position = e.get_position();
var fileID = parseInt(item.getAttribute("fileID"));
var menuID = parseInt(container.getAttribute("MenuID"));
var row = position;

var created = svrCreateReal.createReelfileCollection(6, menuID, fileID);

alert(created );

}

thnks very much in advance

cheers

Pay attention on Web.config of your application. It have to contain some declarations. Similarly to it:

-----

<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections
<system.web>
<pages>
<controls>
<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</controls>
</pages>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="false">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
</system.web
<system.web.extensions>
<scripting>
<webServices>
<!-- Uncomment this line to customize maxJsonLength and add a custom converter -->
<!--
<jsonSerialization maxJsonLength="500">
<converters>
<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>
</converters>
</jsonSerialization>
-->
<!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
<!--
<authenticationService enabled="true" requireSSL = "true|false"/>
--
<!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved
and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and
writeAccessProperties attributes. -->
<!--
<profileService enabled="true"
readAccessProperties="propertyname1,propertyname2"
writeAccessProperties="propertyname1,propertyname2" />
-->
</webServices>
<!--
<scriptResourceHandler enableCompression="true" enableCaching="true" />
-->
</scripting>
</system.web.extensions
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated" />
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode"
type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>
</system.webServer>
</configuration>

Invoke Javascript method after partial postback ?

Howdy All,

This might be an easy one but I don't seem to be able to figure it out.

Basically I have an update panel which contains a button that causes the partial postback. After the postback I'd like it to call a Javascript method.

I tried looking to set it on an atlas object and I even tried some hacks like putting a literal control in the Update Panel and setting the text to <script language=""Javascript"">testFunction();</script>" but still no cigar.

Any help would be greatly appreciated.

Try registering your script on the server-side, during the partial post. The script you register will be executed when the response is received from the partial postback.

Page.ClientScript.RegisterStartupScript

-Tony


in my callback function i havestring js = "alert('s');"; Page.ClientScript.RegisterStartupScript(GetType(), "key", js);It doesn't fire

casaubon:

in my callback function i have string js = "alert('s');"; Page.ClientScript.RegisterStartupScript(GetType(), "key", js); It doesn't fire

You need to wrap your js in <script type="text/javascript"> </script>, or use the overloaded RegisterStartupScript specifying "true" for the last parameter.

Hope this helps,

-Tony

Invoke JavaScript method after partial post back

  Hi,
 Im trying to invoke a javascript method after a partial update has occured from an update panel. I found some code for which i beleive works for an old version, but it doesn't work for the released version.
 Does anyone know how i can invoke a javascript method after post back?
 
 
1 function afterPostback()2 {10 }1112 function PageRequestManagerPropertyChanged(sender, args)13 {14if (args.get_propertyName() =="inPostBack")15 {16if (!$object("_PageRequestManager").get_inPostBack())17 afterPostback();18 }19 }2021 function pageLoad()22 {23 $object("_PageRequestManager").propertyChanged.add(PageRequestManagerPropertyChanged);24 }

You can handle _endRequest to do that. Try something like this:

var prm = Sys.WebForms.PageRequestManager.getInstance();prm.add_endRequest(afterPostback);function afterPostback(sender, args){}

Note: This code has to be placed below the ScriptManager control.


Brilliant, that works a treat.

Thanks

Invoke ModalPopupExtenders .Show() method within javascript code

Hi,

A colleague wrote an extender to validate an address (street name & postal code). He uses a web service for that BUT he didn't write the necessary code in the _onMethodFailed method !

In this method I woud like to display a popup containing 'yes' and 'no' buttons (to ask the customer if he wants to keep the wrong values or no).

Currently, I have an asp:panel with a ModalPopupExtender (which ID is 'AddressErrorPopup') linked on it.

So, in the address validation extender I would like to do something like that :

_onMethodFailed: function(err, response, context) { if (this._popupValue) { var popupExtender = $get(this._popupValue); if (popupExtender) popupExtender.Show(); }}


the _popupValue variable contains 'AddressErrorPopup' : the ID of the ModalPopupExtender.

Problem : the $get(this._popupValue) returns null...

Is there a way to invoke the Show() method of a ModalPopupExtender in the javascript code of an other extender on the same form ?

Thx for your answer...

You need to use $find instead of $get to locate ajax controls. $get is for DOM elements.

Also, make sure that the modalpopup has BehaviorID="AddressErrorPopup" as the actual client ID may be different depending on the naming container hierarchy. Store the BehaviorID in the _popupValue property and then you should find the popup OK regardless of the naming container.


Ok !

Thank you for your help, this is exactly what I needed.

I'm not good at javascript/ajax, sorry for thatConfused


No need to be sorry...that's an easy mistake to make. Working with the ajax toolkit, you will become good at javascriptSmile

Wednesday, March 21, 2012

Is AutoCompletes ContextKey property useless....?

.....or is it just me? I tried using this property, and yes, the string does get passed to the web service method. However, when I check HttpContext.Current.Items, any objects which had previously been added to the Items collection no longer exist. Even HttpContext.Current.Session is null. This makes sense because, with each user keypress, a new HttpContext is created to invoke the web method.

There seem to be no events that can be handled before the web method is invoked. So my question is, exactly what functionality, aside from allowing a user to pass an additional string paramter to the web method, does this property provide? If there's a configuration setting I need to change, or if I'm going about this in totally the wrong way, someone please tell me! I'm using build 10618 of the toolkit.

Thanks,

Dan

Hi Danludwig,

Sometimes we want to send back more parameters instead of the TextBox's value only. For example, if one WebMethod services serverl AutoCompleteExtenders, we need more parameters to Distinguish different Controls or useages. We can past more parameters by using ContextKey. For example: "parameter1, parameter2,parameter3...";

To set its ContextKey, we can use $find("AutoCompleteExtenders.BehaviorID").set_contextKey("your parameters"); Here is the sample.

<%@. Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server"></script><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title>Untitled Page</title> <style> .AutoExtender { font-family: Verdana, Helvetica, sans-serif; font-size: .8em; font-weight: normal; border:solid 1px #006699; line-height:20px; padding:2px; background-color:White; } .AutoExtenderList { border-bottom:dotted 1px #006699; cursor:pointer; color:Maroon } .AutoExtenderHighlight { color:White; background-color:#006699; cursor:pointer; } </style></head><body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TextBox ID="TBSearch" runat="server"></asp:TextBox> <asp:Panel runat="server" ID="myPanel" Height="100px" ScrollBars="Vertical"> </asp:Panel> <ajaxToolkit:AutoCompleteExtender runat="server" ID="autoComplete1" BehaviorID="myACEBID" TargetControlID="TBSearch" ServicePath="AutoComplete.asmx" ServiceMethod="GetCompletionList" MinimumPrefixLength="1" CompletionListCssClass="AutoExtender" CompletionListItemCssClass="AutoExtenderList"
 CompletionListHighlightedItemCssClass="AutoExtenderHighlight" CompletionInterval="1" EnableCaching="true" CompletionSetCount="12" CompletionListElementID="myPanel"UseContextKey="true" ContextKey="Jonathan"/> <br/><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <input id="Button1" type="button" value="button" onclick="changeContextKey()"/> <script type="text/javascript" language="javascript"> function changeContextKey(){ $find("myACEBID").set_contextKey('tommy'); } </script> </form></body></html>

<%@. WebService Language="C#" Class="AutoComplete" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class AutoComplete : System.Web.Services.WebService {

public AutoComplete()
{
}

[WebMethod]
public string[] GetCompletionList(string prefixText, int count, string contextKey)
{
//do something on contextKey to get the parameters.
if (count == 0)
{
count = 10;
}

if (prefixText.Equals("xyz"))
{
return new string[0];
}

Random random = new Random();
List<string> items = new List<string>(count);
for (int i = 0; i < count; i++)
{
char c1 = (char)random.Next(65, 90);
char c2 = (char)random.Next(97, 122);
char c3 = (char)random.Next(97, 122);

items.Add(prefixText + c1 + c2 + c3);
}

return items.ToArray();
}

}

I hope this help.

Best regards,

Jonathan