[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JSON - pass param?

Posted on 2011-10-06
4
Medium Priority
?
402 Views
Last Modified: 2012-05-12
 $.ajax({
            type: "POST",
            url: "WebGlobalMethods.asmx/NameIsAvailable",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg)
            {
                if (msg.d != "false")
                {
                    $("tr:not(:has(th),:contains('" + msg.d + "'))", "table[id*='gvShipping']").hide();
                }
            },
            error: LoadFailed
        });

Open in new window




How do I pass a string into the method NameIsAvailable?

also...wow would I pass multiple parameters?
0
Comment
Question by:Tom Knowlton
  • 3
4 Comments
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 36927389
data: { "same_variable_name1": "Hello World",  "same_variable_name2": true,  "same_variable_name3": 10  },

server side :

public static string NameIsAvailable(string same_variable_name1, bool same_variable_name2, int same_variable_name3)
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36927434
I guess I am still doing something wrong...

my jquery:
$(document).ready(function ()
{
    var loc = window.location.href;

    if (loc.indexOf("CreateNewUser") != -1)
    {
        // alert("check if user name avail");

        $.ajax({
            type: "POST",
            url: "WebGlobalMethods.asmx/NameIsAvailable",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: { "newusername": "max" },
            success: function (msg)
            {
                alert("success");
            },
            error: function (msg)
            {
                alert("failed:  " + msg.d);

            }
        });
    }
});

Open in new window



my web method (web service call):
  [WebMethod]
        public bool NameIsAvailable(string newusername)
        {
            bool nameavail = false;

            if (newusername != "")
            {
                connectToDB myDB = new connectToDB();
                object myObject = new
                {
                    LOGIN = newusername
                };
                string strPickDataReturn = myDB.getStringResult(1, "IWEB.CHECK.USERNAME", myObject);

                XmlDocument doc = new XmlDocument();
                string test = strPickDataReturn;

                doc.LoadXml(strPickDataReturn);

                System.Data.DataSet tmpDS = new DataSet();
                System.IO.StringReader stream;
                stream = new System.IO.StringReader(doc.InnerXml);
                tmpDS.ReadXml(stream);

                if (tmpDS.Tables[0].TableName == "ERRORS")
                {
                    nameavail = false;
                }
                else
                {
                    nameavail = true;
                }
            }

            return nameavail;
        }//end of web method

Open in new window




In my jquery, this part is getting called:

  error: function (msg)
            {
                alert("failed:  " + msg.d);

            }


The message that appears at runtime is

failed : undefined


Is there a way to make the message more verbose?
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 36927588
I needed single quotes around the data:

wrong:   data: { "newusername" : "max" },

correct:   data: '{ "newusername" : "max" }',


$(document).ready(function ()
{
    var loc = window.location.href;

    if (loc.indexOf("CreateNewUser") != -1)
    {
        // alert("check if user name avail");

        var msg;

        $.ajax({
            type: "POST",
            url: "WebGlobalMethods.asmx/NameIsAvailable",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{ "newusername" : "max" }',
            success: function (msg)
            {
                alert("success");
            },
            error: function (msg)
            {
                alert("failed:  " + msg.d);

            }
        });
    }
});

Open in new window

0
 
LVL 5

Author Closing Comment

by:Tom Knowlton
ID: 36927590
thx!
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question