如何在没有数据的情况下获取ajax中的值

时间:2016-07-12 04:32:49

标签: json ajax

$.ajax({
    type: 
    url: 
    data: 
    contentType: 
    dataType: 
    success: function() {
}
})

上面的代码是ajax语法。我的问题是如何在没有数据attriute的情况下获取ajax中的值?

3 个答案:

答案 0 :(得分:1)

您可以使用type: "GET"并移除data

$.ajax({
 url: 'test.php',
 type: 'GET' //default 
 success: function(data){
   //do something with data
   console.log(data);
 }
 error: function(errHere){
  console.log(errHere.responseText);
}
});

答案 1 :(得分:0)

data属性是可选的。你可以删除它。

        public class Cashier {
        private  int numberOfItems;
        private  double totalSum;
        private  double amount;
        private  String names;
        private  String s, name;
        private double price, tendered, change,  dollars, quarters, dimes,   
        nickels, pennies;

        NumberFormat nf = NumberFormat.getInstance();
        DecimalFormat df = (DecimalFormat)nf;

        public Cashier(){
           this.name = "";
           this.price = price;
           price = 0;
           this.s = "";
         }
        public Cashier(String name, double price, String s)
        {
          //this.tendered = 0;
          this.name= name;
          this.price = price;
          //amount = tendered;
          //price = 0;
          //this.s = s;

           }


          public double average()
           {
              return totalSum/numberOfItems;
           }
           public void add(String name, double price)
           {
           totalSum = totalSum + price;
           s = s + name + "........" + price + "\n";
            numberOfItems++;
              }

             public void makeChange()
            {

             change = amount - totalSum;
             change = 100 * change;
             change = Math.round(change);
             change = change / 100;
             dollars = (int)(amount - totalSum) * 100 / 100;
             pennies = (int)(change * 100) % 100;
             quarters = (int)pennies / 25;
             pennies = (int)pennies % 25;
             dimes = (int)pennies / 10;
             pennies = (int)pennies % 10;
             nickels = (int)pennies / 5;
             pennies = (int)pennies % 5;
             pennies = (int)pennies;
              }
             public String getNames()
             {
               return name;
              }
                  public double getPrices()
                    {
                        return price;
                            }
                public double getTotal()
                    {
                       return totalSum;
                               }
                             public double getMoney()
                              {
                          return tendered;
                           }
                            public double getChange()
                             {
                      return tendered - totalSum  ;
                                   }
                           public double getQuantity()
                              {
                return numberOfItems;
                 }
                  public double getAverage()
                           {
                      return average();
                           }
                public double getDollars()
                   {
                    return dollars;
                            }
                        public double getQuarters()
{
return quarters;
}
public double getDimes()
{
return dimes;
}
public double getNickels()
{
return nickels;
}
public double getPennies()
{
return pennies;
}

public void tendered(double amount)
{
 // double tendered;
  tendered = amount;
}

}



public class TestCashier {


public static void main(String[] args) {

Cashier c = new Cashier();

String name = GetData.getWord("Enter name of item");
double price = GetData.getDouble("Enter price of item");
c.add(name, price);

name = GetData.getWord("Enter name of item");
price = GetData.getDouble("Enter price of item");
c.add(name, price);
name = GetData.getWord("Enter name of item");
price = GetData.getDouble("Enter price of item");
c.add(name, price);

// Add a two more entries of your own

// Now average the price of the items
c.average();

// Make payment
double amount = GetData.getDouble("Enter amount of money for payment");
c.tendered(amount);
//ndered(amount); // Twenty dollars were tendered

c.makeChange();

generateReceipt(c);

}
public static void generateReceipt(Cashier c)
{

String s= "ABC Groceries Shop \n";

s = s + "Welcome – thanks for stopping, \n";

DateFormat df = DateFormat.getDateInstance();

Date d = new Date();

NumberFormat f = NumberFormat.getCurrencyInstance();

s = s + "Today is " + df.format(d) + "\n";

s = s + "Item:" +(c.getNames());
       //\n";


s = s + c.getNames() + "..... " + f.format(c.getPrices()) + "\n" + c.getNames() +
        "..... " + f.format(c.getPrices()) + "\n" + c.getNames() + "....." +
        f.format(c.getPrices()) + "\n";

s = s + "____________________" + "\n";

s = s + "Total " + f.format(c.getTotal()) + "\n\n";

s = s + "Amount tendered " + f.format(c.getMoney()) + "\n";

s = s + "The change is " + f.format(c.getChange()) + "\n";

s = s + "There were " + c.getQuantity() + " items" + "\n";
s = s + "The average price is " + f.format(c.getAverage()) + "\n\n";

s = s + "The change includes :" + "\n";

s = s + c.getDollars() + " dollars" + "\n" + c.getQuarters()+ " quarters" +
        "\n" + c.getDimes()+ " dimes" + "\n" + c.getNickels()+ " nickels" + 
        "\n" + c.getPennies() + " cents";

JTextArea text = new JTextArea(s,15, 25);

JScrollPane pane = new JScrollPane(text);

JOptionPane.showMessageDialog(null,pane, "Your bill",
        JOptionPane.INFORMATION_MESSAGE);

}


}




public class GetData {
    static String str;
static double getDouble(String s)
{
str = JOptionPane.showInputDialog(s);
return Double.parseDouble(str);
}
static int getInt(String s)
{
str = JOptionPane.showInputDialog(s);
return Integer.parseInt(str);
}
static String getWord(String s)
{
//str = JOptionPane.showInputDialog(s);
return JOptionPane.showInputDialog(s);
}


}

您可以阅读有关语法here

的更多信息

答案 2 :(得分:0)

语句需要一个值和一个逗号后面的"键入:"," url:"除此之外:为什么你不直接跳过"数据:" -line当你不需要它时?它用于向服务器端发送参数,大多数情况下,它用作数据库查询的参数或类似参数。你不需要发送它。

相关问题