none
my query of wcf POST is not working, i am stuck in that? help needed RRS feed

  • Question

  • http://social.msdn.microsoft.com/Forums/en-US/133c5ee0-0bae-45df-9978-4c025961874e/when-i-am-use-get-function-it-works-fine-but-when-i-user-post-function-error-always-comes?forum=wcf

    i am still getting the error

    when i use GET, this work awesome.

    but instead of only changing GET into POST,

    method 404 not found 

    Thursday, October 2, 2014 9:01 AM

Answers

  • The best way is to work with Fiddler. You can download it for free here:
    http://www.telerik.com/fiddler

    When I work with Web Api I use fiddler to create custom GET and POST and I can browse not only the result of the WCF service but also inspect the header.
    If you prefer you can accomplish the same result with HttpClient.

    For a Get you must use this code:

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders
        .Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.GetAsync(uri).Result;
    return response;

    And for a Post this one:

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders
        .Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.PostAsJsonAsync(uri,parameter).Result;
    return response;
    
    In my case I am forcing the Header to send and return JSON but you can also work with text/xml


    MCAD - MCSD MCTS Sharepoint 2007 MCTS SQL Server 2005 MCTS TFS 2010 IASA Architect blog: http://blog.raffaeu.com "If my answer is useful remember to mark it as useful".

    Friday, October 3, 2014 6:57 AM

All replies

  • when i am using the GET function it is working perfect, but when i am using POST function an error arises, any special reason for that?

    what should i do now?

    Friday, September 19, 2014 10:57 AM
  •  

    an error arises

    And what error would that be?

    Friday, September 19, 2014 6:23 PM
  • when i am using POST, it is not getting into function, and just print the error section, ,

    it works perfect with GET but error with POST

    function GetMeterStandardData(tx)
            {
                 $.support.corps = true;
                  $.ajax({
                      type: 'GET', 
                      crossDomain: true,
                      url: 'xxxxxxxxx',
                      contentType: "application/javascript",
                      data: '{"value1":"1971"}',
                      dataType: "json",
                      processData: false,
                      async : false,
                      success: function (response) {
    var resp = response.xxxxxxx
                          
                           alert(JSON.stringify(resp));
                           $.each(resp, function () {                                            
                           tx.executeSql("INSERT INTO meterstandard(ms_meterstandard,ms_currency,ms_value,ms_denomination,ms_type,ms_linenumber,ms_name,ms_default,ms_order) VALUES('"+ this.MeterStandard +"', '"+ this.Currency +"', '"+ this.Value +"', '"+ this.Denomination +"','"+ this.Type +"','"+ this.LineNumber +"','"+ this.Name +"','"+ this.MSDefault +"','"+ this.Order +"')");
                            alert("idhar se ghoom k gaya hoon ghabrao mat MeterStandard table loaded");
                            
       
       
                        });
                         
                         
                          
                      },
                      error: function ServiceFailed(xhr, textStatus, errorThrown)// When Service call fails
                      {
                          alert(JSON.stringify(errorThrown));
                          alert("jatoi");
                      }
                  });

            }

    Monday, September 22, 2014 1:14 PM
  • when i am using POST, it is not getting into function, and just print the error section, ,

    it works perfect with GET but error with POST

    function GetMeterStandardData(tx)
            {
                 $.support.corps = true;
                  $.ajax({
                      type: 'GET', 
                      crossDomain: true,
                      url: 'xxxxxxxxx',
                      contentType: "application/javascript",
                      data: '{"value1":"1971"}',
                      dataType: "json",
                      processData: false,
                      async : false,
                      success: function (response) {
    var resp = response.xxxxxxx
                          
                           alert(JSON.stringify(resp));
                           $.each(resp, function () {                                            
                           tx.executeSql("INSERT INTO meterstandard(ms_meterstandard,ms_currency,ms_value,ms_denomination,ms_type,ms_linenumber,ms_name,ms_default,ms_order) VALUES('"+ this.MeterStandard +"', '"+ this.Currency +"', '"+ this.Value +"', '"+ this.Denomination +"','"+ this.Type +"','"+ this.LineNumber +"','"+ this.Name +"','"+ this.MSDefault +"','"+ this.Order +"')");
                            alert("idhar se ghoom k gaya hoon ghabrao mat MeterStandard table loaded");
                            
        
       
                        });
                         
                         
                          
                      },
                      error: function ServiceFailed(xhr, textStatus, errorThrown)// When Service call fails
                      {
                          alert(JSON.stringify(errorThrown));
                          alert("jatoi");
                      }
                  });

            }
    Monday, September 22, 2014 1:15 PM
  • when i am using POST, it is not getting into function, and just print the error section

    Maybe Houdini can pull the error out of the hat for you. Without some kind of error messgae being posted, who can help you?

    Monday, September 22, 2014 1:39 PM
  • What should you do now?

    http://www.codeproject.com/Tips/213007/Debug-WCF-REST-Service

    No one can do it for you but you.

    Monday, September 22, 2014 5:19 PM
  • IService.

    <OperationContract()> _
             <WebInvoke(Method:="POST",
                       ResponseFormat:=WebMessageFormat.Json)>
        Function GetData(ByVal value1 As String) As String

    web.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>

      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
            <add name="Access-Control-Allow-Methods" value="POST,GET" />
            <add name="Access-Control-Max-Age" value="1728000" />
          </customHeaders>
        </httpProtocol>
        <modules runAllManagedModulesForAllRequests="true" />
        <!--
            To browse web app root directory during debugging, set the value below to true.
            Set to false before deployment to avoid disclosing web app folder information.
          -->
        <directoryBrowse enabled="true" />
      </system.webServer>
      <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5" />
        <authentication mode="None"></authentication>
      </system.web>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="ServiceBehavior">
              <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="EndpBehavior">
              <enableWebScript/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <bindings>
          <webHttpBinding>
            <binding name="CrossDomain" crossDomainScriptAccessEnabled="true" />
          </webHttpBinding>
        </bindings>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
        <standardEndpoints>
          <webScriptEndpoint>
            <standardEndpoint name="" crossDomainScriptAccessEnabled="true" />
          </webScriptEndpoint>
        </standardEndpoints>
        <services>
          <service behaviorConfiguration="ServiceBehavior" name="WcfService3.Service1">
            <endpoint address="" behaviorConfiguration="EndpBehavior" binding="webHttpBinding" bindingConfiguration="CrossDomain" name="EP" contract="WcfService3.IService1" />
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8100/" />
              </baseAddresses>
            </host>
          </service>
        </services>
            <!--<protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
          <add binding="webHttpBinding" scheme="http"/>
        </protocolMapping>-->    
      </system.serviceModel>
    </configuration>

    jquery calling

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <input id="Button1" type="button" value="button" />
    </body>
    </html>
    <script type="text/javascript" src="jquery-2.0.0.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Button1").click(function () {
                $.ajax({
                    type: "POST",
                    url: "http://localhost/WcfService3/Service1.svc/GetData",
                    contentType: "application/javascript; charset=utf-8",
                    processData: false,
                    dataType: "jsonp",
                    //data: JSON.stringify('{value1:1971}'),
                    data: '{"value1":"1971"}',
                    success: function (response) {
                        var resp = response.GetDataResult
                        alert(resp);
                    },
                    failure: function (response) {
                        //alert(response);
                        var resp = response.statusText
                        alert(resp);
                    },
                    error: function (response) {
                        //alert(response);
                        var resp = response.statusText
                        alert(resp);
                    }
                });
            })
        })
    </script>

    help help needed 

    error: object object

    when i convert it into string

    readystate :4

    status :404

    statusText: error 


    Thursday, September 25, 2014 11:07 AM
  • error: object object

    when i convert it into string

    readystate :4

    status :404

    statusText: error 

    Thursday, September 25, 2014 11:55 AM
  • Hi Miskeen Ali Jatoi,

    Please try to modify your JS code as following:

          $(document).ready(function () {
                $("#Button1").click(function () {
                    $.ajax({
                        type: "POST",
                        url: "http://localhost/WcfService3/Service1.svc/GetData",
                        contentType: "application/json; charset=utf-8",
                        processData: true,
                        dataType: "json",
                        //data: JSON.stringify('{value1:1971}'),
                        data: '{"value1":"1971"}',
                        success: function (response) {
                            alert(JSON.stringify(response));
                            //var resp = response.GetDataResult
                            //alert(resp);
                        },
                        failure: function (response) {
                            //alert(response);
                            var resp = response.statusText
                            alert(resp);
                        },
                        error: function (response) {
                            //alert(response);
                            var resp = response.statusText
                            alert(resp);
                        }
                    });
                })
            })

    Then it works well on my side.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, September 26, 2014 3:32 AM
    Moderator
  • Hi Miskeen Ali Jatoi,

    Please try to modify your JS code as following to see if it works:

    function GetMeterStandardData(tx)
             {
                  $.support.corps = true;
                   $.ajax({
                       type: 'GET', 
                       crossDomain: true,
                       url: 'xxxxxxxxx',
                       contentType: "application/json",
                       data: '{"value1":"1971"}',
                       dataType: "json",
                       processData: true,
                       async : false,
                       success: function (response) {                      
                            alert(JSON.stringify(response));
                            $.each(response, function () {                                            
                            tx.executeSql("INSERT INTO meterstandard(ms_meterstandard,ms_currency,ms_value,ms_denomination,ms_type,ms_linenumber,ms_name,ms_default,ms_order) VALUES('"+ this.MeterStandard +"', '"+ this.Currency +"', '"+ this.Value +"', '"+ this.Denomination +"','"+ this.Type +"','"+ this.LineNumber +"','"+ this.Name +"','"+ this.MSDefault +"','"+ this.Order +"')");
                             alert("idhar se ghoom k gaya hoon ghabrao mat MeterStandard table loaded");
                             
         
        
                         });
                          
                          
                           
                       },
                       error: function ServiceFailed(xhr, textStatus, errorThrown)// When Service call fails
                       {
                           alert(JSON.stringify(errorThrown));
                           alert("jatoi");
                       }
                   });
    
             } 

    Besides, please also try to check the reply in your other thread:
    http://social.msdn.microsoft.com/Forums/en-US/133c5ee0-0bae-45df-9978-4c025961874e/when-i-am-use-get-function-it-works-fine-but-when-i-user-post-function-error-always-comes?forum=wcf .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 26, 2014 9:03 AM
    Moderator
  • GET function is working absolutely fine.

    but the problem is with POST.


    Friday, September 26, 2014 9:39 AM
  • still the same error nothing changes

    readystate :4

    status :404

    statusText: error ,

    any suggestion?


    Friday, September 26, 2014 9:40 AM
  • Hi Miskeen Ali Jatoi,

    Please try to check my project in here: http://1drv.ms/1ywvEZz and it works very well on my side.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Monday, September 29, 2014 9:27 AM
    Moderator
  • when i use GET, this work awesome.

    but instead of only changing GET into POST,

    method 404 not found in the browser.

    i am proving the parameters in the browser, i am really stuck in this, help me Amy Peng

    i have used your project same thing happens as i have described.
    Tuesday, September 30, 2014 9:40 AM
  • Hi Miskeen Ali Jatoi

    I see that you meet the method 404 not found in the browser, when you are proving the parameters in the browser for the POST method as described in this thread(http://social.msdn.microsoft.com/Forums/en-US/133c5ee0-0bae-45df-9978-4c025961874e/when-i-am-use-get-function-it-works-fine-but-when-i-user-post-function-error-always-comes?forum=wcf ), unlike the GET method, we can not browser the POST the method in the browser. We can only get the POST result by using the Jquery or the HttpWebRequest or the HttpClient.

    Then have you checked my project in here(http://1drv.ms/1ywvEZz )? It works well in my side.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 3, 2014 5:38 AM
    Moderator
  • it gives me the same error in the jquery as well.

    now what can i do?

    Friday, October 3, 2014 5:48 AM
  • The best way is to work with Fiddler. You can download it for free here:
    http://www.telerik.com/fiddler

    When I work with Web Api I use fiddler to create custom GET and POST and I can browse not only the result of the WCF service but also inspect the header.
    If you prefer you can accomplish the same result with HttpClient.

    For a Get you must use this code:

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders
        .Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.GetAsync(uri).Result;
    return response;

    And for a Post this one:

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders
        .Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    var response = client.PostAsJsonAsync(uri,parameter).Result;
    return response;
    
    In my case I am forcing the Header to send and return JSON but you can also work with text/xml


    MCAD - MCSD MCTS Sharepoint 2007 MCTS SQL Server 2005 MCTS TFS 2010 IASA Architect blog: http://blog.raffaeu.com "If my answer is useful remember to mark it as useful".

    Friday, October 3, 2014 6:57 AM