0

I try post data with REST API using jQuery AJAX. My code is below,

$.ajax({
    url: 'myurl',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(jsonData),
    dataType: 'jsonp',
    success: function(responseData, textStatus, jqXHR) {   
        if (responseData.result == "true") {            
            $.mobile.changePage("#registersuccess",{transition:"slide"});
        } else {
            alert("kayıt başarısız");
        }
    }
});

I am monitoring with Explorer developer tools. I get this error message:

HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax.
GET - http:MyService?callback=jQuery111306711937631005869_1470230696599&[{"name":"","phoneNumber":"","password":""}]&_=1470230696600

What does this mean: &_=1470230696600?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

3 Answers3

1

Cache defaults to false for JSONP requests (see dataType in your code); the parameter _ is used to burst the cache. The value is the timestamp at the time of the request.

See jQuery docs at http://api.jquery.com/jQuery.ajax/

Luca
  • 9,259
  • 5
  • 46
  • 59
0

Replace datatype from jsonp to json. You can read more about the difference between json and jsonp here What are the differences between JSON and JSONP?

Community
  • 1
  • 1
Poly
  • 417
  • 2
  • 12
  • When I change from json to jsonp , I take Cross domain error. – Salih Yakıcı Aug 03 '16 at 13:57
  • Can you specify if the client and server are on the same domain? If they are not you should look into enabling CORS on your server. – Poly Aug 03 '16 at 14:03
  • No , My server and client does not same domain . I am using Amazon Linux Server , How I enable , except I added my jquery ajax code cache:true; I got this error message : GET - http:MyService?callback=jQuery111306711937631005869_1470230696599&[{"name":"","phoneNumber":"","password":""}] – Salih Yakıcı Aug 03 '16 at 14:06
  • I am not sure which server you are using here, but a Google search on Amazon Linux Server returned this: http://docs.aws.amazon.com/AWSEC2/latest/APIReference/cors-support.html It states that CORS is enabled by default on the AWS. If this is not the server you are using, please provide more info about the server application you are using: Is it Apache Tomcat? IIS? Glassfish? each have their own settings for CORS – Poly Aug 03 '16 at 18:13
  • I solved my problem , Thanks for your helps. Solution is below – Salih Yakıcı Aug 04 '16 at 13:29
  • I see you're using JAX-RS in Java, I recently used the same thing and solved the exact same issue using a similar filter to the one you implemented below – Poly Aug 04 '16 at 13:50
0

I solved Problem adding Server Site code ,

public class CORSFilter

implements ContainerResponseFilter {

public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
        throws IOException {

    MultivaluedMap<String, Object> headers = responseContext.getHeaders();

    headers.add("Access-Control-Allow-Origin", "*");
    //headers.add("Access-Control-Allow-Origin", "http://podcastpedia.org"); //allows CORS requests only coming from podcastpedia.org       
    headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");          
    headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, X-Codingpedia");
}

}