2

I am trying to sending an ajax request from javascript to get data from the salesforce using the following code, but I keep getting Error:0 message. It seems my request is not sending properly. I do not know what is wrong with my code.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script type="text/javascript"> var $j = jQuery.noConflict(); $j(function(){ var url = 'https://myorg-developeredition.na14.force.com/services/apexrest/clsapi'; $j.ajax({ url: url, type: 'POST', success : function() { alert('Hello'); }, error : function(jqXHR, textStatus, errorThrown) { alert('Error: ' + jqXHR.status); }, }); }); </script> 

But when I type the same url in the browser, I got the response from the salesforce. It shows

<response>myresponse</response> 

Thanks

1 Answer 1

1

You are sending a cross domain request which your browser is preventing due to the Same Origin Policy.

To send a cross domain request, you must use jsonp.

var url = 'https://myorg-developeredition.na14.force.com/services/apexrest/clsapi'; $j.ajax({ url: url, type: 'POST', dataType: 'jsonp', success : function() { alert('Hello'); }, error : function(jqXHR, textStatus, errorThrown) { alert('Error: '+jqXHR.status); } // <-- remove the trailing comma you had here }); 
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your prompt response. I modified my code as you suggest now i am getting Error:200 message.
200 means the post was succesfull in some form ( webdesign.about.com/cs/http/p/http2xx.htm ) you should check the data/errortext that your ajax-call receives . Most probably, it will say that the returned format is not jsonp (not 100% sure so be sure to check it).
It shows parseerror because my response in xml format. Is there any way to convert the response to JSON format?
@user1311776 I'm afraid not, the response format is determined by the server you are requesting from. It would probably be best for you to contact SalesForce directly to ask them if they have a data feed which is browser compatible.
@Rory McCrossan : Thanks for your response
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.