DirectEvents and WebApi json request call

  1. #1

    DirectEvents and WebApi json request call

    Hi I am trying to make a post to a webapi control in a MVC application using DirectEvents on a button as below

         <ext:FormPanel 
                ID="FormPanel1" 
                runat="server" 
                Title="Process" 
                Icon="ServerStart"
                BodyPadding="6">
                <Items>
                    <ext:DateField  
                        ID="txtDate" 
                        runat="server" 
                        FieldLabel="Run Date" 
                        IndicatorIcon="Date"
                        Anchor="-5"
                        SelectedDate=""
                        Format="dd-MM-yyyy"
                        InvalidText= "Invalid Format. dd-MM-yyyy"
                        IndicatorTip="Choose execution date"
                        />
                 </Items>
             <BottomBar>
                 <ext:Toolbar runat="server">
                     <Items>
                        <ext:Button runat="server" Icon="LightningGo" Text="Run" Region="West">
                            <Listeners>
                                <Click Handler="
                                    if (!#{txtDate}.validate()) {
                                        Ext.Msg.alert('Error','All fields are both required');
                                        return false; 
                                    }" />
                            </Listeners>
                            <DirectEvents>
                                <Click Url="~/api/Process/PostStart" Timeout="60000" Method="POST" Json="True" Success="Ext.Msg.show({title: 'Server Status',msg: result,buttons: Ext.Msg.OK,icon: Ext.MessageBox.INFO});">
                                    <Confirmation ConfirmRequest="true" Title="Please Confirm" Message="Start workflow process?" />
                                </Click>
                            </DirectEvents>
                        </ext:Button>
                    </Items>
                 </ext:Toolbar>
             </BottomBar>
         </ext:FormPanel>
    as you see i have set Json=True in Click Tag, however the header request still has xml in it

    Click image for larger version. 

Name:	9-5-2012 5-46-25 PM.png 
Views:	150 
Size:	31.6 KB 
ID:	4733

    content type has "application/json" but i think since Accept parameter has application/xhtml+xml it is getting ignored by webapi engine and thus xml is returned instead of json,

    Click image for larger version. 

Name:	response.png 
Views:	112 
Size:	16.5 KB 
ID:	4734

    any help with this is greatly appreciated

    update http://blogs.msdn.com/b/kiranchalla/...ta-part-1.aspx

    f a Request message is sent with an Accept header and also a Content-Type header, Conneg algorithm will use the Accept header to decide about the Response media type and the MediaTypeFormatter to write.
    This should make sense to you as here the Client is asking for a Response in a specific format.

    Example:

    Request:

    POST http://kirandev10:9090/DefaultConNeg...Tests/PostData HTTP/1.1
    Accept: application/xml
    Content-Type: application/json
    Host: kirandev10:9090
    Content-Length: 7
    Expect: 100-continue
    Connection: Keep-Alive

    thank you
    Last edited by Pyropace; Sep 05, 2012 at 10:21 PM.
  2. #2
    Json="true" sets Content-Type only, as you see it equals 'application/json'
    Also Accept contains '*/*' which means all media types

    Therefore the issue is related with your server side logic. Can you post server side code which handles the request?
  3. #3
    Quote Originally Posted by Vladimir View Post
    Json="true" sets Content-Type only, as you see it equals 'application/json'
    Also Accept contains '*/*' which means all media types

    Therefore the issue is related with your server side logic. Can you post server side code which handles the request?
    Hi Vladimir,

    Thanks for your reply, how did you come to the conclusion as it is the server issue ? As i posted in the quote above , if both content and accept are sent via the header, the webapi will go with Accept parameters

    I guess the question would be , how would you clear accept parameters when you work with DirectEvents

    I however have posted the webapi backend code for you , it is responding fine , the problem is just that the response is in xml and not json, but webapi will look at header request to decide what format it should use in response, so the problem is Jason=true will need to modify Header Accept options as well


       public class ProcessController : ApiController
        {
            #region Public Methods and Operators
    
             public LogModel PostStart()
            {
                return new LogModel();
            }
    
            #endregion

    here is what is a screenshot of what is returned

    Click image for larger version. 

Name:	response-02.png 
Views:	121 
Size:	12.7 KB 
ID:	4735
  4. #4
    Well, may be Accept header has a priority over Content-Type in WebAPI (but JSON web service works fine with Content-Type, https://examples2.ext.net/#/Events/D...ds/WebService/)

    If it is true then i can suggest to add Accept header manually (via CustomConfig of direct event)
    <ext:ConfigItem Name="headers" Value="{Accept:'application/json'}" Mode="Raw" />
    or use HttpResponseMessage with 'application/json' media type on the server side

    We will change it in 2.1 release (Accept will be set also, not Content-Type also )
  5. #5
    Great thank you for your reply

Similar Threads

  1. Replies: 0
    Last Post: Jun 06, 2012, 7:38 PM
  2. [CLOSED] DirectEvents call method codebehind
    By rnfigueira in forum 1.x Legacy Premium Help
    Replies: 8
    Last Post: Sep 05, 2011, 3:03 PM
  3. Error 404.15 on AjaxMethod.Request with JSon
    By Spootnick in forum 1.x Help
    Replies: 1
    Last Post: Jun 16, 2011, 4:31 PM
  4. Non JSON ajax request
    By craig2005 in forum 1.x Help
    Replies: 0
    Last Post: Dec 31, 2010, 6:08 PM
  5. [1.0] - JSON Serialize DateTime Request
    By drkoh in forum 1.x Legacy Premium Help
    Replies: 3
    Last Post: Jul 26, 2010, 1:56 PM

Tags for this Thread

Posting Permissions