0

I am getting some errors when trying to send JSON to my server, and I just want to make sure I have this part right.

The part I'm worried about is the data parameter. The data parameter is dynamic, and I just want to make sure that my method below is a valid way of forming it.

I form it like this: dataObj[itemName] = itemValue;

Here is how I implement:

var itemName = "";
var itemValue = "";
var dataObj = {};

if (divId == "CustomerDiv") {
   itemName = "CustomerId";
   itemValue = id;
} else {
   itemName = "OwnerId";
   itemValue = id;
}
var ajaxMethod = "http://localhost:50151/api/webmethod/";
dataObj[itemName] = itemValue;
$.ajax({
   type: "PATCH",
   url: ajaxMethod,
   dataType: "json",
   data: dataObj
});

Would this be a valid way of forming the data parameter?

Thanks!

SkyeBoniwell
  • 6,345
  • 12
  • 81
  • 185

2 Answers2

1

I don't see anything wrong with the way you are constructing the dynamic parameter. Have you tried debugging to make sure the object looks correct prior to your ajax call? It should be a simple object of the form {'CustomerId': val} or { 'OwnerId': val }.

I noticed a post which may be related. It says that "PATCH" is not available in all version of jQuery. It also says not all browsers support patch.

What version of jQuery are you using? What is your browser and version? What errors do you receive?

Community
  • 1
  • 1
Chris Wininger
  • 651
  • 8
  • 13
1

You do are using the correct synthax to generate your data object. The result of your dataObj would be {CustomerId: XX} (for example).

Note that type: "PATCH" is not correct as type is requiering a correct HTTP method (like GET, POST ...).

Pid59
  • 26
  • 1