2

I can't get a WCF call to work from jquery while passing two parameters. If I change the code up a bit to pass just one things work ok.

The Javascript:

 $.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb
    url: "/Services/JobNumberService.svc/GetActiveJobNumberByCustomerOrJointBilling", // Location of the service
    data: '{"customerId": "' + customerId + '", "departmentId": "' + departmentId + '"}', //Data sent to server
    contentType: "application/json; charset=utf-8", // content type sent to server
    dataType: "json", //Expected data format from server
    processdata: true, //True or False
    success: function (msg) {//On Successfull service call
        fillDropDownFromLookupList(msg, jobNumberDropDownId);

        prependItemToDropDown('', 'NONE', jobNumberDropDownId); //Add blank item to top of list
    },
    error: ServiceFailed// When Service call fails
});

Service Signature:

public LookupList GetActiveJobNumberByCustomerOrJointBilling(int customerId, int departmentId)

It's got to be something with how I'm formatting the json that's passed in. It's valid according to JSONLint but maybe not what .net expects.

Ideas are appreciated.

EDIT

This is what I get back in the response

HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/11.0.0.0
Date: Thu, 28 Feb 2013 20:30:17 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Length: 0
Connection: Close

O, I also tried to turn off just my code debugging to track down what the error was but I wasn't seeing any exceptions for some reason.

9
  • Try stepping through the server-side execution and see what the 500 error is - or turn on verbose error messages in your configuration so that the response will contain the stack trace. That's fairly ambiguous. Commented Feb 28, 2013 at 20:56
  • Hey Tony, where's the verbose error setting you're thinking of? I have includeExceptionDetailInFaults="true" in the serviceBehaviors config section. Commented Feb 28, 2013 at 21:00
  • You should be able to get them turned on by editing web.config as follows: <system.web><customErrors mode="On" /></system.web> - If that doesn't work, let me know and I'll help you dig in further. Commented Feb 28, 2013 at 21:10
  • Good idea, I had that flipped to on already sadly Commented Feb 28, 2013 at 21:14
  • Gah - that's what I get for typing quickly. customErrors needs to be set to Off in order to get verbosity. Commented Feb 28, 2013 at 21:43

2 Answers 2

5

Your service takes int values as parameters, but you are sending it strings. If customerId and departmentId are numbers, you need to remove the quotes around them so that they'll be interpreted as such.

'{"customerId": ' + customerId + ', "departmentId": ' + departmentId + '}'
Sign up to request clarification or add additional context in comments.

4 Comments

Didn't think of that, I always use JSON.stringify to generate correct JSON.
I don't think that's the issue. #1, when I just pass one of the parameters the servers gets called ok. #2 I tried it out and didn't work. Let me update the question with what I see error wise
@AndrewWalters does your service expect json? Do you get the 500 error when you send the values as numbers?
Ya, same error if you pass the data is as you suggested. I'm sure I'm doing something stupid just need some extra eyes to figure it out
4

Based on your comments, I believe your problem may be that you're trying to bind more than one parameter to your endpoint. I'm no WCF expert, but I know that this doesn't work in WebAPI.

The fix, in general, is to create a model for your binding to parse into:

public class CustomerModel { 
    public int customerId; 
    public int departmentId; 
}

Then make that the expected parameter of your call:

public LookupList GetActiveJobNumberByCustomerOrJointBilling(CustomerModel model)

Now you just need to drop the quotes from around your JSON object that you're submitting, and your AJAX should work as-is, because the following JSON object will correctly de-serialize into your new CustomerModel object:

{ "customerId": 1, "departmentId": 1 }

If you keep the quotes, you will likely get erroneous results - because the parser will interpret the data it's receiving as a string, rather than a JSON object.

1 Comment

I'll take it from you marking my answer correct that this helped. :) Glad I could be of assistance!

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.