Sending Ajax Request With A Rest Works But Not With Jquery
Solution 1:
If your data
property is a JavaScript object, jQuery converts it to form data for transmission. It's possible that Postman is sending it as an actual string. Try wrapping your data
in single quotes:
data: '{"sitename": "AAGx", "zone": 12, "sector": 34, "square": 7}', ...
Edit: In response to your edits, I can almost guarantee you it is a CORS, or cross-origin resource sharing, issue. AJAX and JavaScript in general are subject to various security constraints. One of the most central security concepts that limits the abilities of JavaScript is the Same Origin Policy. The idea is to prevent XSS (cross-site scripting) attacks.
jQuery's AJAX function will give an error code of 0 if it experiences CORS issues.
There's a detailed answer about how local files are under more strict security constraints here that explains your situation perfectly.
Your solutions are to send the Access-Control-Allow-Origin
header from the server, if you have control of the server. Otherwise, you can try using JSONP which does not use the XMLHTTPRequest
object and is not subject to those same security constraints.
Post a Comment for "Sending Ajax Request With A Rest Works But Not With Jquery"