Solved

creating generic ajax method

Posted on 2014-03-26
1
403 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
ID: 39956119
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript Regex 7 20
disadvantage of code behind appoarch in asp.net 3 28
Glitching Slide Show 19 26
JS to redirect to prev page 8 16
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

685 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