Click here to Skip to main content
15,891,849 members
Home / Discussions / JavaScript
   

JavaScript

 
AnswerRe: Adblock system Pin
Richard MacCutchan12-Feb-14 21:46
mveRichard MacCutchan12-Feb-14 21:46 
AnswerRe: Adblock system Pin
Sibeesh KV29-Sep-14 2:02
professionalSibeesh KV29-Sep-14 2:02 
Questioncopy items of one dropdownlist to another one with jquery Pin
H.Goli11-Feb-14 6:38
H.Goli11-Feb-14 6:38 
AnswerRe: copy items of one dropdownlist to another one with jquery Pin
Kornfeld Eliyahu Peter11-Feb-14 6:54
professionalKornfeld Eliyahu Peter11-Feb-14 6:54 
QuestionCreating a static Map Pin
mrkeivan7-Feb-14 19:34
mrkeivan7-Feb-14 19:34 
GeneralRe: Creating a static Map Pin
Sunasara Imdadhusen21-May-14 23:43
professionalSunasara Imdadhusen21-May-14 23:43 
AnswerRe: Creating a static Map Pin
Sibeesh KV29-Sep-14 2:04
professionalSibeesh KV29-Sep-14 2:04 
QuestionBad Request Call WCF Service Jquery Ajax Pin
Ericsson Alves6-Feb-14 15:32
Ericsson Alves6-Feb-14 15:32 
HI,
I'm trying to get a WCF service to a JSON for Jquery Ajax, but the method is not even to be initiated now returns the error Bad Request.

Below is the Java Script code calling the service, Service Interface, Class of Service, the service Web.config, Global.asax service.

Detail if I access the GetData method for $.getJSON works, but at $.Ajax not.

Already with the AddStudant method does not work somehow.

Could anyone help telling me what's wrong?

JavaScript

JavaScript
$("#bt").click(function () {

var url = "http://localhost:8318/Service1.svc/AddStudant";
var data = { "ID": 1, "Name": "Ericsson Alves" };
var jdata = {};
jdata.student = data;

callAjax(url, 'json', 'POST',
                function (result) {
                    alert(result);
                },
                JSON.stringify(jdata)
                );
});

function callAjax(ajaxUrl, ajaxDataType, ajaxType, funcSucess, dataValues) {

        $.ajax({
            url: ajaxUrl,
            dataType: ajaxDataType,
            type: ajaxType,
            data: dataValues,
            processdata: true,
            contentType: "application/json;charset-uf8"
        })
            .done(function (data) {
                funcSucess(data);
            })
            .always(function (data) {

            }).fail(function (xhr, status, errorThrown) {
                alert(xhr.responseText);
            });
    }


Interface

C#
[ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json)]
        Student GetData(int id);

        [OperationContract]
        [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
        Student AddStudant(Student student);
        
    }

[DataContract]
    public class Student
    {
        [DataMember]
        public int ID { get; set; }
        [DataMember]
        public string Name { get; set; }
    }


Service Class

C#
[AspNetCompatibilityRequirements(RequirementsMode
    = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {
        [WebInvoke(Method = "GET",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json)]
        public Student GetData(int id)
        {
            return new Student() { ID = id, Name ="Ericsson Alves" };
        }

        [WebInvoke(Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json)]
        public Student AddStudant(Student student)
        {
            return new Student() { ID = student.ID , Name ="Ericsson Alves" };
        }
    }


Web.config Service

XML
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>

    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBdg" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" />
      </webHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="BehaviorDefault">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="AJAXWCFServiceAspNetAjaxBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>

    <services>
      <service name="TestWCFJsonAjax.Service.Service1" behaviorConfiguration="BehaviorDefault">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="WebHttpBdg"
                  behaviorConfiguration="AJAXWCFServiceAspNetAjaxBehavior"
          name="Service1" contract="TestWCFJsonAjax.Service.IService1" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    
    <standardEndpoints>
      <webHttpEndpoint>
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" crossDomainScriptAccessEnabled="true" />
      </webHttpEndpoint>
    </standardEndpoints>
    
  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
    <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="GET, POST, OPTIONS" />-->
      </customHeaders>
    </httpProtocol>
  </system.webServer>

</configuration>


Global.asax Service

C#
public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(Service1)));
        }
        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //HttpContext.Current.Response.Cache.SetNoStore();

            //EnableCrossDmainAjaxCall();
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
    }

Ericsson Alves

QuestionSolve datetime country wise to display? Pin
pkarthionline6-Feb-14 0:46
pkarthionline6-Feb-14 0:46 
AnswerRe: Solve datetime country wise to display? Pin
Garth J Lancaster6-Feb-14 0:55
professionalGarth J Lancaster6-Feb-14 0:55 
AnswerRe: Solve datetime country wise to display? Pin
Richard Deeming6-Feb-14 2:08
mveRichard Deeming6-Feb-14 2:08 
QuestionSyntax error on IE10 but working fine on IE 8... Pin
Member 102794055-Feb-14 19:57
Member 102794055-Feb-14 19:57 
AnswerRe: Syntax error on IE10 but working fine on IE 8... Pin
Richard Deeming6-Feb-14 2:05
mveRichard Deeming6-Feb-14 2:05 
QuestionPopulating Input Fields Using Button UIs Pin
ASPnoob3-Feb-14 21:14
ASPnoob3-Feb-14 21:14 
AnswerRe: Populating Input Fields Using Button UIs Pin
Richard Deeming4-Feb-14 3:14
mveRichard Deeming4-Feb-14 3:14 
GeneralRe: Populating Input Fields Using Button UIs Pin
ASPnoob9-Feb-14 18:57
ASPnoob9-Feb-14 18:57 
Questionjava script project - algebric expressions Pin
chgovind2-Feb-14 0:22
chgovind2-Feb-14 0:22 
AnswerRe: java script project - algebric expressions Pin
Richard MacCutchan2-Feb-14 1:38
mveRichard MacCutchan2-Feb-14 1:38 
QuestionFailed to load resource: the server responded with a status of 405 (Method Not Allowed) Pin
Vimalsoft(Pty) Ltd30-Jan-14 8:25
professionalVimalsoft(Pty) Ltd30-Jan-14 8:25 
AnswerRe: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) Pin
Kornfeld Eliyahu Peter2-Feb-14 1:30
professionalKornfeld Eliyahu Peter2-Feb-14 1:30 
AnswerRe: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) Pin
swatishri20-Aug-20 15:19
swatishri20-Aug-20 15:19 
GeneralRe: Failed to load resource: the server responded with a status of 405 (Method Not Allowed) Pin
Vimalsoft(Pty) Ltd20-Aug-20 23:53
professionalVimalsoft(Pty) Ltd20-Aug-20 23:53 
QuestionNo 'Access-Control-Allow-Origin' header is present on the requested resource Pin
Vimalsoft(Pty) Ltd30-Jan-14 1:42
professionalVimalsoft(Pty) Ltd30-Jan-14 1:42 
AnswerRe: No 'Access-Control-Allow-Origin' header is present on the requested resource Pin
Kornfeld Eliyahu Peter2-Feb-14 1:29
professionalKornfeld Eliyahu Peter2-Feb-14 1:29 
AnswerRe: No 'Access-Control-Allow-Origin' header is present on the requested resource Pin
Doug Twyman3-Feb-14 18:28
Doug Twyman3-Feb-14 18:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.