Solved

creating generic ajax method

Posted on 2014-03-26
1
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

734 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