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!

1
  • Shouldn't type be one of GET, POST, PUT or DELETE ? Commented Apr 24, 2013 at 14:11

2 Answers 2

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 ...).

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.