Link to home
Start Free TrialLog in
Avatar of scm0sml
scm0sml

asked on

using jquery inside a javascript function.

Hi,

I have the following javascript function which is inside an include file within my asp.net website.

function saveTrackingData()
{
//debug to make sure we are hitting this function, and we are!
    alert("hey");

    //Test call to get call to .net working
$.ajax({
        type: "POST",
        url: "~/Index.aspx/HelloWorld",
        data: "{}",
        contentType: "application/json",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            alert(msg);
        }
    });

What I am basically trying to do is within the javascript file use jquery to call my helloworld function just as proof of concept....

I'm fairly new to javascript so am not sure if this is along the right lines...

Can someone advise as this isn't working?

Thanks in advance.
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

url: "~/Index.aspx/HelloWorld",

is this jax call is hitting this asp page.
Since the url you assigned is a relative path, make sure that the URI http://yourdomain/~/Index.aspx/HelloWorld exists.
Avatar of scm0sml
scm0sml

ASKER

chaituu:I presume you are asking if the hello world function is on the current page?

Yes the page we are on is index.aspx...

And yes stringray it does exist....
<WebMethod(EnableSession:=False)> _
    Public Shared Function HelloWorld() As String
        Return "Hello: " & DateTime.Now.Millisecond
    End Function
removed data option.check it now;

$.ajax({
        type: "POST",
        url: "~/Index.aspx/HelloWorld",
        contentType: "application/json",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            alert(msg);
        }
  error: function (msg) {
                     alert("error");
        }

    });

use firebug net tab to see what your code is trying to access

I think you should remove the ~/ from the url to make it work
Avatar of scm0sml

ASKER

still no joy i'm afraid?!
Avatar of scm0sml

ASKER

something to add....

index is at the root level... the js file which gets included is in a folder "js" which is in the root folder...
Use this

url: "/Index.aspx/HelloWorld"

instead of

url: "~/Index.aspx/HelloWorld"
Avatar of Kiran Sonawane
this url: "~/Index.aspx/HelloWorld"

could be

 url: "Index.aspx/HelloWorld",

I guess tilde(~) sign is not recognized by JS compiler
Avatar of scm0sml

ASKER

still not working.....

Try this

<WebMethod(EnableSession:=False)> _
    Public Shared Function HelloWorld() As String
        Return "{Hello: " & DateTime.Now.Millisecond & "}"
    End Function

Open in new window

Avatar of scm0sml

ASKER

Still no joy.

I have stripped my code down to about two files and attached a zip file.

I have renamed the ext txt....

Hopefully this will allow you to have a play rather than guessing?

If you need any more info let me know?
testJquery.txt
what do you get from the net tab in firebug?
What if you get the page with this code

$.ajax({
        type: "POST",
        url: "~/Index.aspx/HelloWorld",
        data: "{}",
        contentType: "text/html",
        dataType: "html",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            alert(msg);
        }
    });

Open in new window

Or try this

$.ajax({
        type: "POST",
        url: "Index.aspx/HelloWorld",
        data: "{}",
        contentType: "text/html",
        dataType: "html",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            alert(msg);
        }
    });

Open in new window

Avatar of scm0sml

ASKER

stringray your last post gave me a load of html back but I couldnt debug into the function in .net?

Is it worth you having a go of the project I gave?
Since I'm not much familiar to .NET, I'm not if you have to set the returned content type as "text/javascript" or "application/json".

See this link for further detail.
http://stackoverflow.com/questions/894828/creating-a-json-header-on-asp-net
Yes, my last post queries for a html page instead of json page. This is my intention to check if your page returns what you expect. You have said it returns html code instead of json like {Hello:155351123}. This means there is something wrong with your page.
ASKER CERTIFIED SOLUTION
Avatar of scm0sml
scm0sml

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Check what you have received in your full website. You can use network tab in the firebug or Network tab in google chrome's developer tools to inspect these.
Avatar of scm0sml

ASKER

I'm stuck on a council network at the moment that is blocking me from downloading firebug or chrome.....

Will have to have a look once i've managed to get access....
You are using IE? which version? if 9, there is the similar feature in Network tab in Developer Tools in IE9.
Avatar of scm0sml

ASKER

stuck with 7......

Flaming councils!!

Have got xp so can't upgrade
Ok. Can you tell me your full website address of "Index.aspx/HelloWorld"?
Avatar of scm0sml

ASKER

uSING THIS i GOT IT TO WORK EVENTUALLY.
Avatar of scm0sml

ASKER

Can we get this sorted so I can ask another question?