Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys

I have problem how to send data from Jquery to asp service

always give me an error

Failed to load resource: the server responded with a status of 500 (Internal Server Error) in Console


this is the code

JavaScript
var Info = $("#basketInfo").val();
var text = "some thing";
$.ajax({
                    url: 'Services/WebServiceAjax.asmx/InsertMethod',
                    type: 'POST',
                    dataType:"json",
                    contentType: "application/json; charset=utf-8",
                    
                    data: JSON.stringify({
                        "Items": text,
                        "BasketInfo": Info
                    }),// "{'Items':'" + text + "','basketInfo':'" + basketInfo + "'}",
                    //data: "{'Items':'" + text + "','basketInfo':'" + basketInfo + "'}",// "{'Items':'" + text + "','basketInfo':'" + basketInfo + "'}",
                    //async: false,
                    success: function (response) {
                        alert("Record Has been Saved in Database");
                    },
                    error: function (xhr, ajaxOption, thrownError)
                    { alert(thrownError); console.log('there is some error'); }
                });


and Service
C#
[WebMethod]
   public static string InsertMethod(string Items, string BasketInfo)
   {
       try
       {
           string[] alldata = Items.Split(',');
           string[] baskInfo = BasketInfo.Split(',');
           foreach (var item in alldata)
           {
               string[] Allitems = item.Split('_');
               string itemValue = Allitems[0];
               string itemID = Allitems[1];

               Database Db = new Database();
               string CycleDesignID = Db.CustomeQuery("select CycleDesignID from CycleDesign order by CycleDesignID desc ")[0][0].ToString();
               Db.CustomInsertData(new Processes.Baskets(baskInfo[0], baskInfo[1], Convert.ToInt32(CycleDesignID), Convert.ToInt32("1")));
               string BasketID = Db.CustomeQuery("select BasketID from Baskets order by BasketID desc ")[0][0].ToString();
               Db.CustomInsertData(new Processes.BasketDetails(Convert.ToInt32(BasketID),
                   Convert.ToInt32(itemID), Convert.ToDouble(itemValue), Convert.ToDouble("3"), Convert.ToInt32("1")));
           }
       }
       catch (Exception ex)
       {
           return ex.Message;
       }
       return "Ok";
   }
Posted
Updated 20-Sep-15 22:09pm
v6
Comments
Thanks7872 21-Sep-15 3:31am    
basketInfo != BasketInfo
Ammar Al-hamdabni 21-Sep-15 3:38am    
sorry i didn't understand
Thanks7872 21-Sep-15 3:52am    
You should take into consideration that parameters in web method are case sensitive.basketInfo != BasketInfo.
Ammar Al-hamdabni 21-Sep-15 4:00am    
I changed basketInfo to Info, but doesn't work
Naveen.Sanagasetti 21-Sep-15 3:44am    
I request you to please search the URL in browser, if it is working fine, then please check the parameters which you passed through Json call, I guess this is case sensitive, as Point out by Rohan Leuva, in method declaration you are using the parameter "BasketInfo" and in JSON call you call like below "basketInfo", I guess this might be problem, Please check this once.

1 solution

Remove the "static" from
C#
public static string InsertMethod(string Items, string BasketInfo)
 
Share this answer
 
v2
Comments
Ammar Al-hamdabni 21-Sep-15 5:00am    
You are right, It appear now, but still give me error to insert data
vagelis1 21-Sep-15 5:01am    
please post your error message
vagelis1 21-Sep-15 5:25am    
in your question you set
Items= "some thing"
on server side you do
string[] Allitems = item.Split('_');
string itemValue = Allitems[0];
string itemID = Allitems[1];

SO Allitems[1] will throw indexoutofrange exception as item.Split('_') will return only one element in Allitems
Ammar Al-hamdabni 21-Sep-15 6:15am    
sorry, I mean in 'Some thing' there is a value that give me from another page.

the error always show me this message:
'Failed to load resource: the server responded with a status of 500 (Internal Server Error)'

vagelis1 21-Sep-15 6:32am    
You should try debuging InsertMethod on the server side. Something raises exception there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900