8 Replies - 1947 Views - Last Post: 08 November 2013 - 04:35 AM Rate Topic: -----

#1 igimanigi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 08

javascript and jquery call to webservice giving error

Posted 04 November 2013 - 10:57 AM

Hello guys. this is my first post. I am working on a javascript to call a web service across another domain. from my own research here is what i have achieved so far;
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>

</head>
<body>
<script>
function fun1() 
{

	$.ajax({    
		    crossDomain: true,
                    type: "GET",
                    url: "http://localhost:29114/WebSite9/Service.asmx/HelloWorld",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",  
                    dataType: "jsonp",
                    success: function(msg) {  
                        //alert("Success: " + msg.d);  
document.getElementById("p1").innerHTML="Success: " + msg.d;
                    },
                    error: function (msg) {
                        //alert("Failed: "+ msg.status + ": " + msg.statusText);
document.getElementById("p1").innerHTML="Failed: "+ msg.status + ": " + msg.statusText;
                    }  
                }); 

}
</script>
<p id="p1">out put here</p>
<button onclick="fun1()">run this</button>
</body>
</html>



Then I have an asp .net webservice with the following serialised code.

 [WebMethod]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public void HelloWorld() {

        StringBuilder sb = new StringBuilder();
        JavascriptSerializer js = new JavascriptSerializer();
        sb.Append("fn(");
        sb.Append(js.Serialize("Hello World"));
        sb.Append(");");

        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.Write(sb.ToString());
        Context.Response.End();
    }



The problem now is when i run my javascript and click on the button, my javascript keeps branching out on the error function. to make matters worse the error code is 200 but the statusText says "success". Can someone help me cross the finishing line. i feel i am close but i am stuck.
thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: javascript and jquery call to webservice giving error

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: javascript and jquery call to webservice giving error

Posted 04 November 2013 - 11:10 AM

Quote

to make matters worse the error code is 200 but the statusText says "success"

well, thatís what a HTTP 200 usually means.

and with regrads to the error function:

http://api.jquery.com/jQuery.ajax/ said:

When data is retrieved from remote servers (which is only possible using the script or jsonp data types), the error callbacks and global events will never be fired.

Was This Post Helpful? 0
  • +
  • -

#3 igimanigi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 08

Re: javascript and jquery call to webservice giving error

Posted 05 November 2013 - 12:52 AM

I have read the article and my code looks consistent with what's being said.
I am still stuck.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: javascript and jquery call to webservice giving error

Posted 05 November 2013 - 01:55 AM

so itís the success function that gives the message?
Was This Post Helpful? 0
  • +
  • -

#5 igimanigi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 08

Re: javascript and jquery call to webservice giving error

Posted 05 November 2013 - 05:10 AM

no, it's the error function that is giving the message "success" and code 200. so i am confused. no matter what i do i am not branching out on the success function.
Was This Post Helpful? 0
  • +
  • -

#6 igimanigi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 08

Re: javascript and jquery call to webservice giving error

Posted 07 November 2013 - 01:27 AM

I have managed to use chrome dev to debug and here is what i have noticed. that my c# function is returning the data very fine because i can actually see the value of the callback function. but now my jquery function i think its not able to parse it because it keeps coming out on the onerror part. but then according to chrome dev its a successful process. i have varied my output from my webservice as follows;
1. {name:"hello"};
2. {"name":"hello"}
3. myFunction{"name":"hello"};

but all these outputs i am not able to handle them. I have also noticed using chrome dev that when i load the script it runs 3 connections to jquery first before i click my run button. one of those requests that it makes to jquery is with this url;

http://ajax.googleap.../jquery.min.map

i checked what data is returned by that. then i took this url and used it in place of mine on my jquery call just to see if i get the same data back. and yes, i get the same data back as what is happening in the first 3 calls when the script is still loading but only i am still not able to parse the response. the message i get is code 200 and message success.
So can someone help me with where i am. i am not able to handle the returning data. please please i need help. i have been on this for almost a week.
Was This Post Helpful? 0
  • +
  • -

#7 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 404
  • View blog
  • Posts: 1,720
  • Joined: 30-April 10

Re: javascript and jquery call to webservice giving error

Posted 07 November 2013 - 11:22 AM

Quote


This is googles maping to the CDN for the jQuery script. its to make the next load faster.
Was This Post Helpful? 0
  • +
  • -

#8 igimanigi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 24-September 08

Re: javascript and jquery call to webservice giving error

Posted 08 November 2013 - 04:32 AM

The point that i am making is that i am testing with other web services to try and isolate my problem. i have also tried with this url;
http://api.wundergro...dar_Rapids.json

and this time i am getting positive results. my jquery is branching out on the success function. so this must mean that my c# web service is not returning data that is serialized properly.
Like i mentioned earlier that i have tried sending out data from my c # program with these variations;

1. {name:"hello"};
2. {"name":"hello"}
3. myFunction{"name":"hello"};

but still i get errors.

can someone help me because i think i am close to the solution.

this is my config file is c #;

<?xml version="1.0"?>
<!-- 
    Note: As an alternative to hand editing this file you can use the 
    web admin tool to configure settings for your application. Use
    the Website->Asp.Net Configuration option in Visual Studio.
    A full list of settings and comments can be found in 
    machine.config.comments usually located in 
    \Windows\Microsoft.Net\Framework\vx.x\Config 
-->
<configuration>
  <appSettings/>
  <connectionStrings/>

  <system.webServer>
    <modules>
      
      <add name="JsonHttpModule.JsonHttpModule" type="JsonHttpModule"/>
    </modules>
  </system.webServer>
  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="102400"/>
      </webServices>
    </scripting>
  </system.web.extensions>
  
  <system.web>
    <!-- 
            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="true" targetFramework="4.0"/>
    <!--
          The <authentication> section enables configuration 
          of the security authentication mode used by 
          ASP.NET to identify an incoming user. 
        -->
    <authentication mode="Windows"/>
    <!--
           The <customErrors> section enables configuration 
           of what to do if/when an unhandled error occurs 
           during the execution of a request. Specifically, 
           it enables developers to configure html error pages 
           to be displayed in place of a error stack trace.

           <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
             <error statusCode="403" redirect="NoAccess.htm" />
             <error statusCode="404" redirect="FileNotFound.htm" />
           </customErrors>
        -->
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>

    <!--<compilation debug="true" targetFramework="4.0" />-->
    <customErrors mode="Off"/>
    <webServices>
      <protocols>
        <add name="HttpGet"/>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  
  </system.web>
  <!-- 
        The system.webServer section is required for running ASP.NET AJAX under Internet
        Information Services 7.0.  It is not necessary for previous version of IIS.
    -->
</configuration>


may be someone can spot something.

This post has been edited by JackOfAllTrades: 08 November 2013 - 04:33 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: javascript and jquery call to webservice giving error

Posted 08 November 2013 - 04:35 AM

The only valid JSON string in your list is #2.

This here:

sb.Append("fn(");
sb.Append(js.Serialize("Hello World"));
sb.Append(");");


Doesn't make sense to me. You can't really serialize a function for sending over JSON.

Haven't used JSON serialization in C#, but you usually serialize an object. Like this.

Seeing as this is less a Javascript question at this point and more a C#/.NET/ASP.NET question, I'm going to move this to ASP.NET.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1