Solved

creating generic ajax method

Posted on 2014-03-26
1
396 Views
Last Modified: 2014-03-26
hi all,

I am trying to create a generic AJAX method for calling on a site

function runAjax(webmethod, fieldname, fieldtext) {
    
    return $.ajax({
        type: "POST",
        url: webmethod,
        data: JSON.stringify({ username: fieldtext }),
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}

Open in new window


the above code works fine, however when I substitute username to fieldname (i.e. to the passed variable, the method doesnt fire?

Is there anyway I can inject the fieldname text into this method?

thanks.
0
Comment
Question by:flynny
1 Comment
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 500 total points
Comment Utility
function runAjax(webmethod, fieldname, fieldtext) {
    var jsonObject= {};
            jsonObject[fieldname] = fieldtext;

    return $.ajax({
        type: "POST",
        url: webmethod,
        data: jsonObject,
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
}
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now