We help IT Professionals succeed at work.

using jquery inside a javascript function.

scm0sml
scm0sml asked
on
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.
Comment
Watch Question

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

is this jax call is hitting this asp page.

Commented:
Since the url you assigned is a relative path, make sure that the URI http://yourdomain/~/Index.aspx/HelloWorld exists.

Author

Commented:
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");
        }

    });

Commented:
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

Author

Commented:
still no joy i'm afraid?!

Author

Commented:
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...

Commented:
Use this

url: "/Index.aspx/HelloWorld"

instead of

url: "~/Index.aspx/HelloWorld"
Kiran SonawaneProject Lead
Top Expert 2011

Commented:
this url: "~/Index.aspx/HelloWorld"

could be

 url: "Index.aspx/HelloWorld",

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

Author

Commented:
still not working.....

Kiran SonawaneProject Lead
Top Expert 2011

Commented:
Show your output from

http://your_domain_name.com/Index.aspx/HellowWorld

OR  if it is in development server

then

http://localhost:yourport/Index.aspx/HellowWorld

Commented:
Try this

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

Open in new window

Author

Commented:
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

Commented:
what do you get from the net tab in firebug?

Commented:
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

Commented:
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

Author

Commented:
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?

Commented:
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

Commented:
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.
Commented:
Just an update:
function OnSuccess(response) {
    alert(response.d);
}

function saveTrackingData()
{
    //Test call to get call to .net working
    alert("hey");

    $.ajax({
        type: "POST",
        url: "Index.aspx/HelloWorld",
        data: '{name: "' + '"hey" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });

Worked in my cut down version.... but when I put it into the full website it doesn;t work so I'm going to have to try and track down why.

Commented:
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.

Author

Commented:
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....

Commented:
You are using IE? which version? if 9, there is the similar feature in Network tab in Developer Tools in IE9.

Author

Commented:
stuck with 7......

Flaming councils!!

Have got xp so can't upgrade

Commented:
Ok. Can you tell me your full website address of "Index.aspx/HelloWorld"?

Author

Commented:
uSING THIS i GOT IT TO WORK EVENTUALLY.

Author

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