Mhmt
Mhmt

Reputation: 769

How to send a parameter in ajax POST?

I tried draw route of click truck marker on google map.But I get this eror "Internal Server Error"

Javascript codes :

  function Route(Param)
      {

          $.ajax({
              type: "POST",
              url: "Mainpage.aspx/Route",
                data: '{"data":"' + Param+ '"}',
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (msg) {

                  if (msg.d) {
                      alert(msg.d);
                  }
                  else {
                      alert("Error...");
                  }
              },
              error: function (XMLHttpRequest, textStatus, errorThrown) {
                  alert("Status: " + textStatus); alert("Error: " + errorThrown);
               }

          });


       //Click truck marker


      // call and set parameter Route function 
        Route(parseInt(dataTruck.TruckId));

Code behind

Mainpage.aspx.cs

    static string _Route;

    [WebMethod]
    public  static string Route (int TruckId)
    {


        _Route = _DrawRoute(TruckId);
        return _Route ;
    }

    public static string _DrawRoute(int TruckId)
    {


     // return Serialize rows
    }

Route method return this format :

[{"Lat":37.9137,"Lng":28.3708},{"Lat":37.9138,"Lng":28.3707},{"Lat":37.9137,"Lng":28.3709},{"Lat":37.9138,"Lng":28.3708}]

Upvotes: 0

Views: 54

Answers (2)

123456
123456

Reputation: 36

here is your content type is json so it necessary to data convert into json format

you can also need convert from json format at web service method so your web service look like

Javascript:

Json_Parameters = JSON.stringify(Param);
$.ajax({
    type: "POST",
    url: url,
    async: false,
    data: JSON.stringify({ "RequestParameters": Json_Parameters }),
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        InputHandler(result);
    }
});

Code-behind :

public  static string Route (string RequestParameters) {
  JavaScriptSerializer ser = new JavaScriptSerializer();
  string objRequestData = ser.Deserialize<string>(RequestParameters);
}

Upvotes: 1

Todd
Todd

Reputation: 5454

it's a bit dificult for me to debug anything that's happening ajax code, since I don't have much of a context.

but...

I do have a couple suggestions. It's not good practice to name variables the same as Javascript constructors. So I'd rename XMLHttpRequest to jqXHR, which is more dogmatic jquery. Also, note that the ajax fn returns an XHR obj, which is a superset of XMLHttpRequest.

Second, you've provided the ?complete return type:

[{"Lat":37.9137,"Lng":28.3708},{"Lat":37.9138,"Lng":28.3707},{"Lat":37.9137,"Lng":28.3709},{"Lat":37.9138,"Lng":28.3708}].

This is an array of objects, so using if(msg.d) { ... } wouldn't make sense to use dot notation in an array (if an array is exactly what you're getting back). Where does the d property come from?

I'd fix those couple of things first. Let me know what your findings are!

Upvotes: 0

Related Questions