Send JQuery JSON to WCF REST using date

前端 未结 6 502
慢半拍i
慢半拍i 2020-12-08 08:54

I know that are a lot of posts about consuming a WCF REST through JQuery/JSON, but I can\'t get it to work. I\'m currently stuck at a date parameter. Below is my C# method:<

6条回答
  •  既然无缘
    2020-12-08 09:20

    Alsalaam Aleykum.

    All you have to do is to respond to the error. I mean change the date format so the json can parse it to the web service.

    your code should look like this:

           function botaoclick() {
    
             var date = new Date();
             date = "\/Date(" + date.valueOf() + ")\/";
             // valueOf() method Returns the primitive value of a Date object.
    
             var datavar = {
               'pDtTimeStampTransac': date,
               'pIDResource': 1,
               'pQty': 1
             };
    
             $.ajax({
               type: "POST",
               contentType: "application/json; charset=utf-8",
               url: "YOUR URL",
               dataType: "json",
               data: JSON.stringify(datavar),
               error: jqueryError,
               success: function(msg) {
                 alert("back");
                 var divForResult = document.getElementById("test");
                 divForResult.innerHTML = "Result: " + msg.d + "";
               }
             })
           }
    
           function jqueryError(request, status, error) {
             alert(request.responseText + " " + status + " " + error);
           }

提交回复
热议问题